How to copy, move, rename, delete files/folders in Python


In this tutorial, we’re gonna look at way to copy, move, rename, and delete files/folders in Python using shutil module.

Related Posts:
How to read/write files in Python

Copy file/folder in Python

Copy file

We use shutil.copy(source, destination) to copy the file at source to destination folder.

– This function returns path of the copied file.
– If destination is a filename, it will be used as the new name of the copied file.

>>> import shutil
>>> shutil.copy('D:\\ozenero\\tutorials-list.txt', 'D:\\ozenero\\Basics\\')

# destination is a filename
>>> shutil.copy('D:\\ozenero\\tutorials-list.txt', 'D:\\ozenero\\Basics\\tutorials.txt')
Copy folder

We use shutil.copytree(source, destination) to copy entire folder (including all folders and files inside) at source to destination folder.

>>> import shutil
>>> shutil.copytree('D:\\Python Files', 'D:\\Python\\Basic Tutorials')
'D:\\Python\\Basic Tutorials'

– This function returns path of the copied folder.
– If destination folder already exists, the function will throw a FileExistsError error.

Move file/folder in Python

We use shutil.move(source, destination) to move file or folder (including all folders and files inside) at source to destination folder.

– This function returns path of new location.
– If destination is a filename or a folder that doesn’t exist, it will be used as the new name of the moved file/folder.
– If there is already a file/folder with the same filename in destination, it will throw an error.
Important: If we move a file to a folder that doesn’t exist, the file WILL be moved and renamed WITHOUT file extension.

>>> import shutil

# file
>>> shutil.move('D:\\tutorials-list.txt', 'D:\\ozenero')
# folder

>>> shutil.move('D:\\Basics', 'D:\\ozenero')

# move file and rename
>>> shutil.move('D:\\tutorials-list.txt', 'D:\\ozenero\\list.txt')

# 'tutorials-list.txt' is already in 'D:\ozenero' folder
>>> shutil.move('D:\\tutorials-list.txt', 'D:\\ozenero')
# shutil.Error: Destination path 'D:\ozenero\tutorials-list.txt' already exists

# 'D:\ozenero\list' doesn't exist
>>> shutil.move('D:\\tutorials-list.txt', 'D:\\ozenero\\list')
# 'tutorials-list.txt' is changed to 'list' (file)

Rename file/folder in Python

We can use shutil.move(source, destination) with the source same as destination to rename the file or folder.

>>> import shutil

# rename file
>>> shutil.move('D:\\ozenero\\Basics\\tutorials.txt', 'D:\\ozenero\\Basics\\posts.txt')

# rename folder
>>> shutil.move('D:\\ozenero\\Basic Tutorials', 'D:\\ozenero\\BasicTuts')

Delete file/folder in Python

Permanent delete

We have 3 functions for specific cases:
os.unlink(path): delete file at path.
os.rmdir(path): delete folder (must be empty) at path.
shutil.rmtree(path): delete folder (including all files and folder inside) at path.

>>> import os
# delete file
>>> os.unlink('D:\\ozenero\\list.txt')

# delete empty folder
>>> os.rmdir('D:\\ozenero\\Basic Tutorials')
# not empty folder -> throw an error
>>> os.rmdir('D:\\ozenero\\BasicTuts')
Traceback (most recent call last):
  File "", line 1, in 
OSError: [WinError 145] The directory is not empty: 'D:\\ozenero\\BasicTuts'

# delete folder with files and subfolders inside
>>> import shutil
>>> shutil.rmtree('D:\\ozenero\\BasicTuts')
Safe delete

Instead of permanently deleting files/folders, we can use third-party send2trash module that will files or folders to trash or recycle bin.

At first, we need to install send2trash module, open cmd, then run:
pip install send2trash

Once the installation is successful, we can see send2trash folder at Python\Python[version]\Lib\site-packages.
Now we’re gonna import the module and use its send2trash() function:

>>> import send2trash
>>> send2trash.send2trash('D:\\ozenero\\tutorials-list.txt')
0 0 votes
Article Rating
Notify of
Newest Most Voted
Inline Feedbacks
View all comments