Raspberry Pi

How to Copy Files in Raspberry Pi Using Python

Raspberry Pi is a Linux-based system and usually Linux lovers use it for programming in Python since it’s the official language for Raspberry Pi. If you are working with the Python programming language and want to copy any file without going back to the terminal or GUI, then there are some python modules that will do the file copy job for you on the Raspberry Pi system.

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:

$ python3

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 os

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:

source = "<Original File path>"

destination = "<Path of the directory or file>"

Example

source = "/home/pi/myfile.txt"

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:

dest = shutil.copy(source, destination)

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:

print("Destination path:", dest)

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.

About the author

Zahra Zamir

An Electronics graduate who loves to learn and share the knowledge, my passion for my field has helped me grasp complex electronics concepts and now I am here to share them with others.