Copy a file from one location to another in Python Copy a file from one location to another in Python python python

Copy a file from one location to another in Python


You have to give a full name of the destination file, not just a folder name.

You can get the file name using os.path.basename(path) and then build the destionation path usin os.path.join(path, *paths)

for item in fileList:    filename = os.path.basename(item[0])    copyfile(item[0], os.path.join("/Users/username/Desktop/testPhotos", filename))


You could just use the shutil.copy() command:

e.g.

    import shutil    for item in fileList:        shutil.copy(item[0], "/Users/username/Desktop/testPhotos")

[From the Python 3.6.1 documentation. I tried this and it works.]


Use os.path.basename to get the file name and then use it in destination.

import osfrom shutil import copyfilefor item in fileList:    copyfile(item[0], "/Users/username/Desktop/testPhotos/{}".format(os.path.basename(item[0])))