Python is a powerful programming language that allows you to work with files & folders from within your application or website. Sometimes you may need to copy files & folders in your python application. You can easily do this using shutil python module. It offers several functions for file operations. It is available by default in every python distribution. You just need to import it before using it. In this article, we will learn how to copy files in python.
How to Copy Files in Python
Here are the steps to copy one or more files in python.
1. Copy single file in Python
In this case, we will use copyfile function. Here is the syntax to use copyfile function.
from shutil import copyfile copyfile(src, dst)
In the above code, both src and dst need to be full path of source and destination files respectively. If dst file exists it will be replaced. Also Python needs to have write permission to destination location, else it will give IOError.
Here is an example to copy file /home/ubuntu/data.txt to /home/projects/new-data.txt
from shutil import copyfile copyfile('/home/ubuntu/data.txt', '/home/projects/new-data.txt')
Alternatively, you can use copy or copy2 functions. They allow you to use directory locations as destination, instead of using filenames.
copy('/home/ubuntu/data.txt', '/home/projects/') copy2('/home/ubuntu/data.txt', '/home/projects/')
Here is a simple table that gives you a summary of the different features of each copy-related function offered by shutil library.
Function | Copies metadata | Copies permissions | Uses file object | Destination may be directory |
---|---|---|---|---|
shutil.copy | No | Yes | No | Yes |
shutil.copyfile | No | No | No | No |
shutil.copy2 | Yes | Yes | No | Yes |
shutil.copyfileobj | No | No | Yes | No |
You can use any of the above functions, depending on your requirement. They all have similar signature where the first argument is the source and the second argument is the destination. But there are some features available in one function but not the others. For example, functions like copy and copyfile do not copy the file metadata such as creation date, modified date, etc. while copy2 does.
2. Copy multiple files in Python
Sometimes you may need to copy multiple files in Python. In this case we will need to loop through the files of the directory and run the above shutil command on each file separately. Here is an example to copy files from /home/ubuntu to /home/projects
import os import shutil src='/home/ubuntu/' dest='/home/projects/' src_files = os.listdir(src) for file_name in src_files: full_file_name = os.path.join(src, file_name) if os.path.isfile(full_file_name): shutil.copy(full_file_name, dest)
3. Copy folder in Python
Sometimes you may want to copy entire folder recursively to another folder. In such cases, use the function copytree offered by distutils library.
Here is an example to copy the entire folder contents of /home/ubuntu/ to /home/projects/ using this function.
from distutils.dir_util import copy_tree # copy subdirectory example fromDirectory = "/home/ubuntu" toDirectory = "/home/projects" copy_tree(fromDirectory, toDirectory)
As you can see, it is very easy to copy one or more files from one directory to another. You need to use the right function depending on your requirement. These lines of code are generally a part of bigger functions or modules. So you can customize them as per your needs.
Also read:
How to Comment in Python
Git Rename Local & Remote Branch
How to Create Remote Git Branch
How to Unstage Files in Git
How to Enable Bluetooth from Command Line in Linux
Related posts:
Sreeram has more than 10 years of experience in web development, Python, Linux, SQL and database programming.