Copying a file from one folder to Another with Python
In this article, we shall discuss copying a file from one folder to another using python functions. A file is a collection of data or information stored on a computer. As we already know, there are different types of files, like audio, video, and text files. These text files are taken in a binary format by a computer. While working with files in the python programming language, we call files as directories, and a file present inside another file is called a subdirectory. Now let us see a few file operations done by python.
File operations using python:
Python contains many methods to perform file or folder operations. We use the OS module, one of the in-built modules of python. This module has various functions used for adding and deleting folders or files, retrieving the data from the files, changing the file location, searching for a file location, and many more operations. To have access to all these functions, we first have to import the module by using the below command
after importing the module, we will use the listdir() method to access the files and fetch their data.
Similar to the OS module, we also use another module called shutil which also provides all types of file operation functions. In addition, it gives the user an option to copy or delete files. To import the shutil module, we use the below commands
import shutil shutil.submodule_name(arguments passed)
In this module, we have functions like shutil.copy(), shutil.copy2(), and shutil.copytree() to copy files from one directory to another directory. Now let us know how we can work with these functions to copy files.
By shutil.copy() operation:
With this function, the file is copied from the source to the destination or target folder. But the permission mode of the file is preserved, and the metadata of the file, such as the date of creation and modification, will not be preserved. The syntax of this shutil.copy() method is as follows
Where origin states the current location of the file and target refers to the destination folder where the file is pasted.
By shutil.copy2() operation:
This method is the same as the shutil.copy() method, but the only difference between this and that method is the shutil.copy() method won't preserve the metadata, but in this method, even the metadata is also preserved along with all other data. Even the execution of this method is similar to the shutil.copy() method, but the fetching way of the directory is different. We give the command for this method as follows
now let us look at an example code using the shutil.copy() method:
# importing the modules import os import shutil # giving the folder path origin = 'C:\Users\hp\OneDrive\Desktop\hello\' target = 'C:\Users\hp\OneDrive\Desktop\java\' # Fetching the list of files files = os.listdir(origin) # Fetching all the files to a directory for file_name in files: shutil.copy(origin+file_name, target+file_name) print("copied required Files successfully")
$ python test.py copied required Files successfully
In this code, we first imported the required modules and then gave the folder paths of the origin and target folders to copy the file. Then we gave a command for fetching the required file and used the shutil.copy() function. At last, we gave a statement to acknowledge that the file had been copied. And after execution of this code, you will find the required output.
By shutil.copytree() method:
This method moves files, and it also moves all the sub-files if present in it. With this, both the source and the destination contain the files. So, the string must include the names of both parameters. The syntax for this method is as follows
After using this method, we get an output where we can see the changes made after the execution where the file name changes to the name, we mentioned in the target file.
With this, we get to cover the process of copying a file from one folder to another folder using the python function. In this, we learned about the basic file structure and operations. To work with python, we discussed two different modules used to file operations. And finally, we saw a few techniques from python modules with an example code for proper understanding.