3/27/2020

Python : How to copy files from one location to another using shutil.copy()

Just check the example code

..

import shutil
# Copy file to another directory
newPath = shutil.copy('sample1.txt', '/home/varung/test')
print("Path of copied file : ", newPath)
#Path of copied file :  /home/varung/test/sample1.txt

#Copy a file with new name
newPath = shutil.copy('sample1.txt', '/home/varung/test/sample2.txt')
print("Path of copied file : ", newPath)
#Path of copied file :  /home/varung/test/sample2.txt

# Copy a symbolic link as a new link
newPath = shutil.copy('/home/varung/test/link.csv', '/home/varung/test/sample2.csv')
print("Path of copied file : ", newPath)
#Path of copied file :  /home/varung/test/sample2.csv

# Copy target file pointed by symbolic link
newPath = shutil.copy('/home/varung/test/link.csv', '/home/varung/test/newlink.csv', follow_symlinks=False)
print("Path of copied file : ", newPath)
#Path of copied file :  /home/varung/test/newlink.csv
..