You are currently viewing Perform High-Level File Operations In Python – shutil Module

Perform High-Level File Operations In Python – shutil Module

Copying or moving files or folders manually from one directory to another directory could be a real pain. This can be automated using a Python module called shutil.

Shutil module provides some high-level operations on files and collection of files like copying, moving, or removing the files.

In other words, the shutil module helps in automating the task of file copying or moving from one directory to another directory. It comes under the Python standard library so it doesn’t need to be installed externally.

In this article, we’ll learn this module.

Copy files to another directory

shutil.copy() method is used to copy the content of the source file to the destination file or directory. While the process of copying also copies the file’s permission mode but other metadata like the file’s creation and modification times, is not preserved.

The source must represent a file but the destination can be a file or a directory. If the destination is a directory then the file will be copied to the destination directory with a base filename from the source. The destination must be writable.

If the destination is a file and already exists then it will be replaced by the source file otherwise the new file will be created.

Syntax: shutil.copy(source, destination, *, follow_symlinks = True)

Parameter:

  • source: A string that shows the path of the source file.
  • destination: A string that shows the path of the destination file or directory.
  • follow_symlinks (optional): The default value of this parameter is True. If it is False and the source represents a symbolic link then the destination will be created as a symbolic link.

Copying source file to the destination

Output

If the destination is the directory

Output

Copy metadata along with the file

shutil.copy2() method is used to copy the contents of the source file to the destination file or directory. It is the same as the shutil.copy() method but the only difference is that this method tries to preserve the metadata of the file.

Syntax: shutil.copy2(source, destination, *, follow_symlinks = True)

Parameter:

  • source: A string that shows the path of the source file.
  • destination: A string that shows the path of the destination file or directory.
  • follow_symlinks (optional): The default value of this parameter is True. If it is False and the source represents a symbolic link then it attempts to copy all metadata from the source symbolic link to the newly-created destination symbolic link. This functionality is platform dependent.

Output

If the destination is a directory

Output

Copy content from one file to another file

shutil.copyfile() method is used to copy the content of the source file to the destination file without the metadata. The source and destination must represent a file and the destination file must be writable. If the destination already exists then it’ll be replaced or a new file will be created.

Syntax: shutil.copyfile(source, destination, *, follow_symlinks = True)

Parameter:

  • source: A string that shows the path of the source file.
  • destination: A string that shows the path of the destination file or directory.
  • follow_symlinks (optional): The default value of this parameter is True. If False and source represent a symbolic link then a new symbolic link will be created instead of copying the file.

Output

If the source and destination file path is the same

If it happens then a SameFIleError will be raised and we can handle the error by using shutil.SameFileError.

Output

Copy the directory tree

shutil.copytree() method is used to replicate the complete directory tree. It recursively copies the entire directory tree rooted at the source directory to the destination directory. The destination directory must not already exist. The new directory will be created during the process.

Syntax: shutil.copytree(src, dst, symlinks = False, ignore = None, copy_function = copy2, ignore_dangling_symlinks = False)

Parameter:

  • src: A string that shows the path of the source directory.
  • dest: A string that shows the path of the destination.
  • symlinks (optional): This parameter accepts True or False, depending on which the metadata of the original links or linked links will be copied to the new tree.
  • ignore (optional): If ignore is given, it must be a callable that will receive as its arguments the directory being visited by copytree(), and a list of its contents, as returned by os.listdir().
  • copy_function (optional): The default value of this parameter is copy2, other copy function like copy() can be used for this parameter.
  • ignore_dangling_symlinks (optional): This parameter value when set to True is used to put a silence on the exception raised if the file pointed by the symlink doesn’t exist.

Output

Remove a directory tree

shutil.rmtree() method is used to delete the entire directory tree, the specified path must point to a directory (but not to a symbolic link to a directory).

Syntax: shutil.rmtree(path, ignore_errors=False, onerror=None)

Parameter:

  • path: A path-like object representing a file path. A path-like object is either a string or bytes object representing a path.
  • ignore_errors: If ignore_errors is true, errors resulting from failed removals will be ignored.
  • oneerror: If ignore_errors is false or omitted, such errors are handled by calling a handler specified by onerror.

Removing a top-level directory tree

Output

Removing a directory tree under a directory

Output

Move files to another location

shutil.move() method is used to move the source file or a directory to the destination location. If the destination directory already exists then the source directory will move inside that directory.

Syntax: shutil.move(src, dst, copy_function=copy2)

Parameter:

src: A string that shows the path of the source file.

dst: A string that shows the path of the destination file or directory.

copy_function(optional): The default value of this parameter is copy2. We can use other copy functions also.

Output

If the destination doesn’t exist

Output

Finding executable files

shutil.which() method is used to find the path to an executable file specified to the cmd argument. If no cmd is specified then it will return None.

Syntax: shutil.which(cmd, mode = os.F_OK | os.X_OK, path = None)

Parameter:

  • cmd: File in string format.
  • mode: This parameter specifies the mode by which the method should execute.
  • os.F_OK tests the existence of the path
  • os.X_OK checks if the path can be executed
  • path: This parameter specifies the path to be used, if no path is specified then the results of os.environ() are used.

Output

More cool methods

There are some other cool methods that the shutil module provides other than the above-mentioned methods.

Copy the stats of a file or a directory using shutil

shutil.copystat() method is used to copy the stats of a file or a directory like permission bits, last access time, last modification time, and flags from the source to the destination.

Output

Retrieve disk usage stats

shutil.disk_usage(path) method is used to get the statistics of disk usage of the specified path. The return value has attributes totalused, and free which are the amount(in bytes) of the total, used, and free space. A path may be a file or a directory.

Output

Archive a directory

shutil.make_archive() is used to create an archive file in a zip or tar format.

Output

Unpacking a directory

shutil.unpack_archive() is used to unpack the archive file. The method takes two arguments, the first is the filename which is the full path of the archive file and the second is the directory in which the files are extracted.

Output

For performing the high-level archiving operations on files in Python, See zipfile module.

Conclusion

We sure learned to handle high-level file operations using the shutil module. We’ve learned how to

  • Copy the file or a directory to another directory
  • Copy the metadata of the file
  • Permanently move the file or a directory to another location
  • Remove and copy the directory tree

Along with these operations, we’ve seen some of the cool methods which are provided by the shutil module.

Reference: docs.python.org/3.10/library/shutil.html


That’s all for now

Keep Coding✌✌