Python Shutil – copy and move files

The Python shutil module is used to perform high level operations on files or collections of files. The shutil module specializes in obtaining information from these collections of files as well as moving and copying them. The python os module has similar functions, but unlike shutil, they are focused on single files.

The first step is to install the Shutil library from the command line.

pip install shutil

If you can use the import shutil command in the IDE, you can be assured it’s installed correctly.


Shutil copy Functions

The shutil.copy() function is used to copy a file from one location to the other. This method only allows for single files to be transferred. If a file with the same name is present in the destination folder, that file will be overwritten. In the case that the destination folder does not exist, an error will be thrown.

If also copies over any permissions that the file may have had, but metadata such as time of creation etc, are not preserved.

shutil.copy("C://Users/CodersLegacy/example.txt", "D://Destination")

The shutil.copy2 is identical to copy, save for the fact that it attempts to preserve the metadata as well. The amount of metadata preserved, varies form operating system to operating system.

The shutil.copyfile function is identical to the copy function, except that it does not copy over permissions of the source file.

The shutil.copytree() function is used to recursively copy entire directories from one location to the other. Any folders within the folder to be copied are also completely copied over including their contents. The destination directory must not already exist.

shutil.copytree("C://OldFolder", "D://NewFolder") 

Using the code above, all files and folders within OldFolder will be copied over to NewFolder.


Shutil Move / Remove Functions

shutil.move(source, destination, copy_function = shutil.copy) 

The shutil.move() function moves a file from one destination to the other. If the destination is a directory, the file is placed within that directory. The way this function moves a file, or collection of files can be changed using it’s 3rd parameter, copy_function.

shutil.move("C://Users/CodersLegacy/file.txt", "D://NewFolder/")

By default, the move() function utilizes the copy2() function. You can change this to either copy() or copytree() if you wish.

The shutil.rmtree() Function, or “remove tree” function is used to delete an entire directory. The path passed to this function must be a directory, and not a file.

shutil.rmtree("C://Users/CodersLegacy/NewFolder")

Any files or folders within the NewFolder will be deleted using the above command.


System related Functions

The make_archive() builds an archive (zip or tar) of files in the root directory. The function takes 2 parameters compulsory parameters and several optional ones. See the syntax below.

shutil.make_archive(base_name, format, root_dir, base_dir)
  • The first, base_name is the name of the file to create, including the path, minus any format-specific extension.
  • The second parameter determines the type of the archive, either zip or tar.
  • root_dir is a directory that will be the root directory of the archive. Basically where the archive is placed after it’s created.
  • base_dir, is the directory where we start archiving from. Both base_dir and root_dir default to the current directory if not specified.

See the ZipFile library which is dedicated entirely to the creation and handling off Zip-files.

The shutil.which() Function returns the path to an executable which would be run if the given cmd was called. If no cmd would be called,  None is returned.

>>> shutil.which("python")
'C:\\Python37\\python.EXE'

The function above finds the path of the executable file for python.

The shutil.disk_usage() function returns the disk usage statistics for a given location. Leaving a '.' as we did below will allow it to return the disk usage stats for the disk drive it’s in. In this case, since our file is located in C drive, the stats for C drive will be returned.

import shutil

total, used, free = shutil.disk_usage('.')
GBdiv = 1024 ** 4

print('Total Storage: {:6.2f} GB'.format(total / GBdiv))
print('Used : {:6.2f} GB'.format(used / GBdiv))
print('Free : {:6.2f} GB'.format(free / GBdiv))

In the above example we do a simple conversion from bytes to Gigabytes by dividing it by 1024 ** 4. The output is hence shown in Gigabytes.

Total Storage:   0.57 GB
Used :   0.34 GB
Free :   0.22 GB

You can use this function to find out the disk usage stats for other file paths as well by passing them as input arguments.


This marks the end of the Python shutil article. Suggestions and contributions for CodersLegacy are more than welcome. Any questions can be asked in the comments section below.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments