Follow this tutorial to learn how to copy files in Raspberry Pi using Python.
How to Copy Files in Raspberry Pi Using Python?
Let’s suppose; I have a file with the name myfile.txt in my Raspberry Pi system, and I want to copy this file to some other location:
To do the file copy process through Python, follow the below-given steps:
Note: You can do this process to copy any files from one place to another through Python.
Step 1: Run Python
To copy the file using Python, first run the Python interpreter using the below-written command:
Usually, Python is pre-installed in Raspberry Pi, so users don’t have to worry about installing it.
Step 2: Import Python Modules
After running Python, now we must import the Python modules. To perform the desired action, we will require two Python modules; one is the os module which allows the system to use functions that are dependent on the operating system, and secondly, we will import shutil module which will aid us in copying files. To import both these Python modules, run the below commands:
import shutil
Step 3: Specify Source and Destination
After importing the required modules, now it is time to specify the source and destination of the file which you want to copy. Basically, the source is the current location of your file and the destination is the location where you want to create a copy of the file:
destination = "<Path of the directory or file>"
Example
destination = "/home/pi/Desktop/myfile(copy).txt"
In the quoted example, I am copying a file which is present at home/pi directory (source location) and now I want to copy it at Desktop location (destination). Also, I have changed the name for the copied version file so that it can be differentiated that it is a copy of the original file, but it is not mandatory you can use the same name at both locations if you want.
Step 4: Use Shutil Function to Copy the File
Once you have specified the source and destination correctly, now use the below-mentioned shutil function to create a copy of the file at a desired location:
Step 5: Verification
After running the shutil.copy command, a copy of the file will be created instantly and you can verify this by running the below-written print command:
The output of the command will print the destination location of the file copy.
Also, since I have copied the file on the desktop, the simplest way is that I can quickly go to the desktop to check it. But in case you haven’t copied the file at the desktop then the above step is recommended for you:
Note: You can also combine the full code mentioned in the above steps and add it into a file with the .py extension and then run the file through python3 interpreter to perform the copy process.
Conclusion
It is a very simple and five-step method to quickly copy your desired files to different locations using Python. Just run Python and import two python modules which are os and shutil then specify the source and destination for the file. After that use shutil.copy function to copy the file and transfer it to any location you want with any file name.