shutil Python
Introduction
In this article you will learn how to work with several files at once using shutil.
Before studying this article I recommend making sure that you are familiar with the material from the article
os
Official documentation
Copying
| copyfile() | copies only the content of the file |
| copy() | copyfile() + permissions + the destination may be a directory |
| copy2() | copy() + metadata (file creation and modification time) |
copyfile
Syntax
shutil.copyfile(src, dst, *, follow_symlinks=True)
Description
Copies the contents (without metadata) of the file named src to the file named dst and return dst in the most efficient way possible. src and dst are path-like objects or path names given as strings.
dst must be the full name of the destination file; see copy() for a copy that accepts a destination directory path. If src and dst point to the same file, SameFileError is raised.
The destination location must be writable; otherwise an OSError exception will be raised. If dst already exists, it will be replaced. Special files such as character or block devices and pipes cannot be copied with this function.
If follow_symlinks is false and src is a symbolic link, then instead of copying the file pointed to by src, a new symbolic link will be created.
Raises an auditing event shutil.copyfile with arguments src, dst.
Changed in version 3.3: IOError used to be raised instead of OSError. Added a new argument: follow_symlinks. It now returns dst.
Changed in version 3.4: SameFileError is raised instead of Error. Since the former is a subclass of the latter, this change is backward compatible.
Changed in version 3.8: Platform-specific fast-copy syscalls may
be used internally for a more efficient copy of the file.
See the section
"Platform-dependent efficient copy operations"
.
Example
shutil/ └── source.txt
cat source.txt
Welcome to www.eth1.ru/www.eth1.ru!
import shutil shutil.copyfile('source.txt', 'destination.txt')
The new file destination.txt will appear in the same directory.
shutil/ ├── destination.txt └── source.txt
You can check the contents with the cat command (in Linux and Unix ) or with the notepad command (in Windows )
cat destination.txt
Welcome to www.eth1.ru/www.eth1.ru!
copy
Syntax
shutil.copy(src, dst, *, follow_symlinks=True)
Description
Copies the file src to the file or directory dst.
src and dst must be path-like objects or strings.
If dst is a directory, the file will be copied into dst, using the base file name from src.
If dst is a file that already exists, it will be replaced. Returns the path to the newly created file.
If follow_symlinks is false and src is a symbolic link, dst will be created as a symbolic link.
If follow_symlinks is true and src is a symbolic link, dst will be a copy of the file referenced by src.
copy() copies the file data and the file's permission mode (see os.chmod()). Other metadata, such as the creation and
modification time of the file, is not preserved.
To preserve all the file metadata from the original, use copy2().
Raises an auditing event shutil.copyfile with arguments src, dst.
Raises an auditing event shutil.copymode with arguments src, dst.
Changed in version 3.3: added the follow_symlinks argument. Now returns the path to the newly created file.
Changed in version 3.8: Platform-specific fast-copy syscalls may be used
internally for a more efficient copy of the file. See the section "Platform-dependent efficient copy operations".
Example
Suppose we are in the directory python:
python/ ├── dir_a │ ├── Armenia.html │ ├── Finland.html │ ├── Spain │ └── Sweden.html └─ dir_b
import shutil import os src = "/mnt/c/Users/Andrei/sandbox/python/dir_a" dest = "/mnt/c/Users/Andrei/sandbox/python/dir_b" dir_a_files = os.listdir(src) print(dir_a_files) dir_b_files = os.listdir(dest) print(dir_b_files) os.chdir(src) for file in dir_a_files: with open(file) as f: print(file, f.read())
python shutil_ex.py
['Armenia.html', 'Finland.html', 'Spain', 'Sweden.html'] ['Armenia.html', 'Finland.html', 'Sweden.html'] Armenia.html tickets to Armenia hotels in Armenia Finland.html tickets to Finland hotels in Finland Traceback (most recent call last): File "/mnt/c/Users/Andrei/sandbox/python/shutil_ex1.py", line 16, in <module> with open(file) as f: ^^^^^^^^^^ IsADirectoryError: [Errno 21] Is a directory: 'Spain'
Opening a directory as a file failed
import shutil import os src = "/mnt/c/Users/Andrei/sandbox/python/dir_a" dest = "/mnt/c/Users/Andrei/sandbox/python/dir_b" dir_a_files = os.listdir(src) print(dir_a_files) dir_b_files = os.listdir(dest) print(dir_b_files) os.chdir(src) for file in dir_a_files: shutil.copy(file, dest)
['Armenia.html', 'Finland.html', 'Spain', 'Sweden.html'] [] Traceback (most recent call last): File "/mnt/c/Users/Andrei/sandbox/python/shutil_ex.py", line 16, in <module> shutil.copy(file, dest) File "/usr/local/lib/python3.11/shutil.py", line 419, in copy copyfile(src, dst, follow_symlinks=follow_symlinks) File "/usr/local/lib/python3.11/shutil.py", line 256, in copyfile with open(src, 'rb') as fsrc: ^^^^^^^^^^^^^^^ IsADirectoryError: [Errno 21] Is a directory: 'Spain'
Despite the error caused by the inability to copy a directory, the regular files that came earlier in the list than Spain had already been copied.
ls dir_b/
Armenia.html Finland.html
To avoid errors caused by directories, let us use a file type check os.paht.isfile which can determine whether a file is a regular file and not a directory.
import shutil import os src = "/mnt/c/Users/Andrei/sandbox/python/dir_a" dest = "/mnt/c/Users/Andrei/sandbox/python/dir_b" dir_a_files = os.listdir(src) print(dir_a_files) dir_b_files = os.listdir(dest) print(dir_b_files) os.chdir(src) for file in dir_a_files: if os.path.isfile(file): shutil.copy(file, dest)
python shutil_ex.py
['Armenia.html', 'Finland.html', 'Spain', 'Sweden.html'] []
ls dir_b/
Armenia.html Finland.html Sweden.html
If there were files in the destination directory with the same names as the ones being copied, they will be overwritten.
move
for file in dir_a_files: if os.path.isfile(file): shutil.move(file, dest)
python shutil_ex.py
['Armenia.html', 'Finland.html', 'Spain', 'Sweden.html'] []
ls dir_a
Spain
ls dir_b
Armenia.html Finland.html Sweden.html
If such files had already been in the destination directory, an error would have occurred
['Armenia.html', 'Finland.html', 'Spain', 'Sweden.html'] ['Armenia.html', 'Finland.html', 'Sweden.html'] Traceback (most recent call last): File "/mnt/c/Users/Andrei/sandbox/python/shutil_ex.py", line 18, in <module> shutil.move(file, dest) File "/usr/local/lib/python3.11/shutil.py", line 823, in move raise Error("Destination path '%s' already exists" % real_dst) shutil.Error: Destination path '/mnt/c/Users/Andrei/sandbox/python/dir_b/Armenia.html' already exists
You can force the files to be overwritten by specifying full paths using os.path.join()
import shutil import os src = "/mnt/c/Users/Andrei/sandbox/python/dir_a" dest = "/mnt/c/Users/Andrei/sandbox/python/dir_b" dir_a_files = os.listdir(src) print(dir_a_files) dir_b_files = os.listdir(dest) print(dir_b_files) os.chdir(src) for file in dir_a_files: if os.path.isfile(file): print(dest) full_dest = os.path.join(dest, file) print(full_dest) print() shutil.move(file, full_dest)
['Armenia.html', 'Finland.html', 'Spain', 'Sweden.html'] ['Armenia.html', 'Finland.html', 'Sweden.html'] /mnt/c/Users/Andrei/sandbox/python/dir_b /mnt/c/Users/Andrei/sandbox/python/dir_b/Armenia.html /mnt/c/Users/Andrei/sandbox/python/dir_b /mnt/c/Users/Andrei/sandbox/python/dir_b/Finland.html /mnt/c/Users/Andrei/sandbox/python/dir_b /mnt/c/Users/Andrei/sandbox/python/dir_b/Sweden.html
ls dir_a
Spain
ls dir_b
Armenia.html Finland.html Sweden.html
Delete the contents of a directory
To clear a directory with Python you need to use shutil and os
import shutil import os def delete_dir_content(path: str): for file in os.listdir(path): file_path = os.path.join(path, file) try: if os.path.isfile(file_path) or os.path.islink(file_path): os.unlink(file_path) elif os.path.isdir(file_path): shutil.rmtree(file_path) except Exception as e: print(f"Failed to delete {file_path}. Reason: {e}")
You can read about deep copy and shallow copy in programming
here
About the function
copy()
in
Python
you can read
here
Article author: Andrei Olegovich