Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Copy a file from one location to another in Python

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.

like image 415
robster Avatar asked Oct 17 '18 09:10

robster


People also ask

How do you copy all files in a folder to another folder in Python?

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?

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.

What is the use of copy2 in Python?

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.

How do I copy a file from one location to another?

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).

How to copy the content of the source file in Python?

The shutil.copy () method in Python is used to copy the content of the source file to destination file or directory.


Video Answer


4 Answers

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.]

like image 161
Z Yoder Avatar answered Oct 18 '22 13:10

Z Yoder


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))
like image 23
Psytho Avatar answered Oct 18 '22 13:10

Psytho


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])))
like image 26
Rakesh Avatar answered Oct 18 '22 13:10

Rakesh


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.

like image 41
Thom Avatar answered Oct 18 '22 14:10

Thom