Copying Files in Python

Copying Files in Python

Copying files is a common operation when developing Python applications. Python provides many ready-made functions and methods for copying files. In this article, we will introduce several methods for copying files in Python.

shutil.copy(src, dest)

The simplest way to copy files in Python is to use the copy() method in the shutil library. This method takes two parameters: the source file and the destination file path.

The following is an example code using the shutil.copy() method to copy a file:

import shutil

src_file = '/home/user/file1.txt'
dst_dir = '/home/user/destination/'

shutil.copy(src_file, dst_dir)

Here, we copy the source file file1.txt from the /home/user/ directory to the /home/user/destination/ directory.

os.system()

In addition to using the shutil.copy() method, we can also use the os.system() method in Python to copy files. The os.system() method works similarly to shell commands. Below is an example code that uses the os.system() method to copy a file:

import os

src_file = ‘/home/user/file1.txt’
dst_file = ‘/home/user/destination/file1.txt’

cmd = “cp ” + src_file + ” ” + dst_file
os.system(cmd)

Here, we use the shell command cp to copy a file.

subprocess.run()

subprocess.run() is another convenient method in Python that can be used to copy files. This method allows us to run a shell command in a subprocess and obtain its output and return code. Below is example code using the subprocess.run() method to copy a file:

import subprocess

src_file = '/home/user/file1.txt'
dst_file = '/home/user/destination/file1.txt'

cmd = ["cp", src_file, dst_file]
subprocess.run(cmd, check=True)

Here, we use the subprocess.run() method to run the cp shell command.

shutil.copy2(src, dst)

Another method that can be used to copy files is shutil.copy2(). It is similar to the shutil.copy() method, but it also copies the source file’s metadata (such as owner, permissions, etc.) to the destination file. The following is an example code using the shutil.copy2() method to copy a file:

import shutil

src_file = '/home/user/file1.txt'
dst_dir = '/home/user/destination/'

shutil.copy2(src_file, dst_dir)

Here, we copy the source file file1.txt from the /home/user/ directory to the /home/user/destination/ directory.

shutil.copytree(src, dst, symlinks=False, ignore=None)

Finally, shutil also provides a method, shutil.copytree(), that can be used to copy an entire directory. This method requires two parameters: the source directory and the destination directory path. If the destination directory does not exist, it will be created automatically. The symlinks parameter controls whether to copy directory symbolic links. The ignore parameter controls which files or directories should be ignored. Below is an example code using the shutil.copytree() method to copy a directory:

import shutil

src_dir = '/home/user/dir1/'
dst_dir = '/home/user/destination/'

shutil.copytree(src_dir, dst_dir)

Here, we copied the directory dir1 from the /home/user/ directory to the /home/user/destination/ directory.

Conclusion

Copying files in Python is a basic and essential operation. This article introduces several of the most commonly used methods for copying files in Python, including shutil.copy(), os.system(), subprocess.run(), shutil.copytree(), and shutil.copy2(). Using one of these methods, you can conveniently copy files from one location to another.

Leave a Reply

Your email address will not be published. Required fields are marked *