In this short guide, you’ll see how to copy a file, from one folder to another, using Python.
To start, here is a template that you may use to copy a file in Python using shutil.copyfile:
import shutil original = r'original path where the file is currently stored\file name.file extension' target = r'target path where the file will be copied\file name.file extension' shutil.copyfile[original, target]
Let’s now see the steps to apply the above template in practice.
Step 1: Capture the original path
To begin, capture the path where your file is currently stored.
For example, let’s suppose that a CSV file is stored in a folder called Test_1:
C:\Users\Ron\Desktop\Test_1\products.csv
Where the CSV file name is ‘products‘ and the file extension is csv.
Step 2: Capture the target path
Next, capture the target path where you’d like to copy the file.
For our example, the file will be copied into a folder called Test_2:
C:\Users\Ron\Desktop\Test_2\products.csv
Step 3: Copy the file in Python using shutil.copyfile
For the final step, use the following template to copy your file:
import shutil original = r'original path where the file is currently stored\file name.file extension' target = r'target path where the file will be copied\file name.file extension' shutil.copyfile[original, target]
Make sure to place the ‘r‘ character before your paths to avoid the following error:
SyntaxError: [unicode error] ‘unicodeescape’ codec can’t decode bytes in position 2-3: truncated \UXXXXXXXX escape
In the context of our example, the complete code would look like this:
import shutil original = r'C:\Users\Ron\Desktop\Test_1\products.csv' target = r'C:\Users\Ron\Desktop\Test_2\products.csv' shutil.copyfile[original, target]
If you run the code in Python [adjusted to your paths], you’ll see that the ‘products‘ CSV file would be copied into the Test_2 folder.
Alternatively, you may copy a file with a new name.
For instance, let’s copy the original CSV file [with the file name of ‘products‘] to the new location with a new file name [‘new_products‘]:
import shutil original = r'C:\Users\Ron\Desktop\Test_1\products.csv' target = r'C:\Users\Ron\Desktop\Test_2\new_products.csv' shutil.copyfile[original, target]
The new file name [called ‘new_products‘] would then be copied in the target location [the Test_2 folder].
The same principles would apply for other file types. For instance, let’s suppose that a JPG file called ‘image‘ is stored in the Test_1 folder.
The following code can then be used to copy the image to the Test_2 folder:
import shutil original = r'C:\Users\Ron\Desktop\Test_1\image.jpg' target = r'C:\Users\Ron\Desktop\Test_2\image.jpg' shutil.copyfile[original, target]
The JPG file should now appear in the Test_2 folder.
Firstly, I made an exhaustive cheatsheet of shutil methods for your reference.
shutil_methods =
{'copy':['shutil.copyfileobj',
'shutil.copyfile',
'shutil.copymode',
'shutil.copystat',
'shutil.copy',
'shutil.copy2',
'shutil.copytree',],
'move':['shutil.rmtree',
'shutil.move',],
'exception': ['exception shutil.SameFileError',
'exception shutil.Error'],
'others':['shutil.disk_usage',
'shutil.chown',
'shutil.which',
'shutil.ignore_patterns',]
}
Secondly, explain methods of copy in exmaples:
shutil.copyfileobj[fsrc, fdst[, length]]
manipulate opened objects
In [3]: src = '~/Documents/Head+First+SQL.pdf'
In [4]: dst = '~/desktop'
In [5]: shutil.copyfileobj[src, dst]
AttributeError: 'str' object has no attribute 'read'
#copy the file object
In [7]: with open[src, 'rb'] as f1,open[os.path.join[dst,'test.pdf'], 'wb'] as f2:
...: shutil.copyfileobj[f1, f2]
In [8]: os.stat[os.path.join[dst,'test.pdf']]
Out[8]: os.stat_result[st_mode=33188, st_ino=8598319475, st_dev=16777220, st_nlink=1, st_uid=501, st_gid=20, st_size=13507926, st_atime=1516067347, st_mtime=1516067335, st_ctime=1516067345]
shutil.copyfile[src, dst, *, follow_symlinks=True]
Copy and rename
In [9]: shutil.copyfile[src, dst]
IsADirectoryError: [Errno 21] Is a directory: ~/desktop'
#so dst should be a filename instead of a directory name
shutil.copy[]
Copy without preseving the metadata
In [10]: shutil.copy[src, dst]
Out[10]: ~/desktop/Head+First+SQL.pdf'
#check their metadata
In [25]: os.stat[src]
Out[25]: os.stat_result[st_mode=33188, st_ino=597749, st_dev=16777220, st_nlink=1, st_uid=501, st_gid=20, st_size=13507926, st_atime=1516066425, st_mtime=1493698739, st_ctime=1514871215]
In [26]: os.stat[os.path.join[dst, 'Head+First+SQL.pdf']]
Out[26]: os.stat_result[st_mode=33188, st_ino=8598313736, st_dev=16777220, st_nlink=1, st_uid=501, st_gid=20, st_size=13507926, st_atime=1516066427, st_mtime=1516066425, st_ctime=1516066425]
# st_atime,st_mtime,st_ctime changed
shutil.copy2[]
Copy with preseving the metadata
In [30]: shutil.copy2[src, dst]
Out[30]: ~/desktop/Head+First+SQL.pdf'
In [31]: os.stat[src]
Out[31]: os.stat_result[st_mode=33188, st_ino=597749, st_dev=16777220, st_nlink=1, st_uid=501, st_gid=20, st_size=13507926, st_atime=1516067055, st_mtime=1493698739, st_ctime=1514871215]
In [32]: os.stat[os.path.join[dst, 'Head+First+SQL.pdf']]
Out[32]: os.stat_result[st_mode=33188, st_ino=8598313736, st_dev=16777220, st_nlink=1, st_uid=501, st_gid=20, st_size=13507926, st_atime=1516067063, st_mtime=1493698739, st_ctime=1516067055]
# Preseved st_mtime
shutil.copytree[]
Recursively copy an entire directory tree rooted at src, returning the destination directory