I have a list called fileList containing thousands of filenames and sizes something like this:
['/home/rob/Pictures/some/folder/picture one something.jpg', '143452']
['/home/rob/Pictures/some/other/folder/pictureBlah.jpg', '473642']
['/home/rob/Pictures/folder/blahblahpicture filename.jpg', '345345']
I want to copy the files using fileList[0] as the source but to another whole destination. Something like:
copyFile(fileList[0], destinationFolder)
and have it copy the file to that location.
When I try this like so:
for item in fileList:
copyfile(item[0], "/Users/username/Desktop/testPhotos")
I get an error like the following:
with open(dst, 'wb') as fdst:
IsADirectoryError: [Errno 21] Is a directory: '/Users/username/Desktop/testPhotos'
What could be something I could look at to get this working? I'm using Python 3 on a Mac and on Linux.
The shutil. copytree() method recursively copies an entire directory tree rooted at source (src) to the destination directory. It is used to recursively copy a file from one location to another. The destination should not be an existing directory.
How to copy files from one folder to another using Python? The shutil module provides functions for copying files, as well as entire folders. For copying multiple files at once, you'll have to have a list of all files you want to copy and loop over them to copy them.
The copy2 () method in Python is used to copy the content of the source file to the destination file or directory. This method is identical to shutil.copy () method also preserving the file’s metadata. In addition to this, the Pathlib module is also incorporated to work with filesystem paths concerned with different operating systems.
src = r’C:\Newfile\file2.txt’ is the source path. file2.txt is the filename that I have created with the .txt extension. dst = r’D:\file2.txt’ destination path. file2.txt is the filename that I have created with the .txt extension. To copy the file from one location to another, we have to use shutil.copyfile (src, dst).
The shutil.copy () method in Python is used to copy the content of the source file to destination file or directory.
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.]
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))
Use os.path.basename
to get the file name and then use it in destination.
import os
from shutil import copyfile
for item in fileList:
copyfile(item[0], "/Users/username/Desktop/testPhotos/{}".format(os.path.basename(item[0])))
You probably need to cut off the file name from the source string and put it behind the destination path, so it is a full file path.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With