首页 > 解决方案 > 在 Python 中将文件从一个位置复制到另一个位置

问题描述

我有一个名为fileList的列表,其中包含数千个文件名和大小,如下所示:

['/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']

我想使用 fileList[0] 作为源复制文件,但复制到另一个完整的目标。就像是:

copyFile(fileList[0], destinationFolder)

并让它将文件复制到该位置。

当我这样尝试时:

for item in fileList:
    copyfile(item[0], "/Users/username/Desktop/testPhotos")

我收到如下错误:

with open(dst, 'wb') as fdst:
IsADirectoryError: [Errno 21] Is a directory: '/Users/username/Desktop/testPhotos'

我可以看看什么来让它工作?我在 Mac 和 Linux 上使用 Python 3。

标签: pythonpython-3.xfile-copying

解决方案


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


推荐阅读