Copy and paste files in Python

How to copy and paste files in Python

The addition of copy and paste functionality to the computer system have saved life’s (especially programmers). We can copy and paste files from one directory to another in Python, I’ve been in a position where I had to move huge amount of files from a directory to another. I decided to automate this process, in this tutorial we will learn how we can copy and paste files from one directory to another. We will achieve this feat using Python’s standard library shutil. Though there are some modules out there that can achieve the same thing, but the all require some basic setup or so.

Getting started:

Shutil module has many methods you can use to copy and paste files. But we will look into this three.

copyfile(src,dst) : This method accepts the src which is the original file and dst which must not be a directory but rather a filename if the dst already exists it will be overwritten by the new file. The destination location must be writable; otherwise, an IOError exception will be raised.

from shutil import copyfile
copyfile(src, dst)

shutil.copy(src,dst) : this method is more like the copy2 method with a slight difference, in the sense that this method doesn’t keep the record of the files metadata.

from shutil import copy
#using filename instead of directory
copy('source.txt', 'destination.txt')
#Using directory instead of filename
copy('/src/dir/test.ext, '/dst/dir/')

copy2(src,dst): This method is often used more than the rest this is because it allows the dst argument to be a directory rather than a file name and also preserves the files metadata.

import shutil
#Using directory instead of filename
shutil.copy2('/src/dir/test.ext, '/dst/dir/')
#using filename instead of directory
shutil.copy2('/src/test.ext', '/dst/dir/newfile.ext')

Real world examples:

How copy and paste files using Python in real world.

copyfile(src,dst):

import shutil

file_path = "/storage/emulated/0/qpython/"
file_dest = "/storage/emulated/0/qpython/"

#SHUTIL.COPYFILE
#shutil.copyfile(file_path+"files.txt",file_dest+"/copyfile/test.txt")
Copy and paste  in Python copyfile
copyfile

shutil.copy(src,dst):

import shutil

file_path = "/storage/emulated/0/qpython/"
file_dest = "/storage/emulated/0/qpython/"

#SHUTIL.COPY
#using filename instead of directory
shutil.copy(file_path+'files.txt',file_dest+'copy/destination.txt')

#Using directory instead of filename
shutil.copy(file_path+'files.txt',file_dest+'copy/')
Copy and paste  in Python copy
copy (1)
Copy and paste  in Python copy
copy (2)

copy2(src,dst):

import shutil

file_path = "/storage/emulated/0/qpython/"
file_dest = "/storage/emulated/0/qpython/"

#SHUTIL.COPY2
#Using directory instead of filename
shutil.copy2(file_path+'files.txt',file_dest+'copy2/')

#using filename instead of directory
shutil.copy2(file_path+'files.txt', file_dest+'copy2/newfile.txt')


The copy2 first method
copy2 (1)
The copy2 second method
copy2 (2)

Conclusion:

You might ask your self why would I need to copy and paste files using Python, the answer is Automation. You can write a program that helps you arrange your movies/videos into categories e.g series,large files, ….etc. or you might want to build a space eating virus just like in this tutorial. Here is the code used in this tutorial.

One comment

Leave a Reply

Your email address will not be published. Required fields are marked *