If you are also wondering how to copy a file in python then leave all your worries behind. We have created this short and quick guide to teach you the same.
Python which is a very popular programming language comes with many modules that can do any all types of file management operations. One such module that we will discuss is the
Why do we need to copy a file in python?
Before, going further if you are a beginner and don’t know why to copy a file in python, leave your burden behind.
Well, for example, if you have a folder in which you have 3 files containing a lot of data, and you want to copy all these files and place them to a new position.
You can do this by manually opening the folder then copying the files and placing somewhere else.
But when you have thousands of files, you will have to search for them manually, which will take your time. In that case, you can use the
shutil module of python to make this task easier.
Now let’s jump straight to the guide for copying a file in python using the “
How to copy files in python?
To keep things simple let’s understand our task.
We have a file name “file1.txt” which is stored inside a folder named “folderA” which itself is stored in another folder named “practice”.
Our task is to copy “file.txt” which is stored in “folderA” and copy it in “folderB”. We will also perform other operations like completely moving the file from “folderA” to “folderB”
As mentioned above, to copy a file in python we will have to use the
shutil module. So, first, we will import the
shutil module by writing:
shutil the module has a method known as
copyfile(). So, first, we will create a variable that will store the initial address on the source address. The following statement will do that:
source_address = r’D:\practice\folderA\file1.txt’
In a similar manner, we will add the destination or the final address in a new variable named destination_address
final_address = r’D:\practice\folderB\file(copy).txt’
Now all you have to do is write the statement below to copy the files from
When you will execute this code the result will be that “file.txt” which was present in “
folderA” is now copied into “
folderB” named as “
Import shutil source_address = r’D:\practice\folderA\file1.txt’ final_address = r’D:\practice\folderB\file(copy).txt’ shutil.copyfile(source_address,final_address)
Wasn’t that very simple?
Well, not only files in txt format, but this method will work completely for all kinds of files whether it is a CSV file, DOCX file, JPEG, or even PNG.
Things to know about the
- If the destination that you are selecting isn’t writable then executing this code will show an IOError exception.
- If the source and the destination address are the same, then it will throw an error. This is because if you are copying a file to another destination using the
shutil.copyfile()method then the addresses should not be the same.
- This function cannot support the copying of the files such as pipes or block devices.
Moving a file to another destination using
In the above case, you can copy a file from one destination to another destination, but if you want to completely move a file then you can use the
We have discussed an easy method to copy a file from source to destination in python.
Let us know if it was a helpful guide for you. Thank you for reading.