Skip to main content
 首页 » 编程设计

python之在 Python 中将文件从一个位置复制到另一个位置

2025年05月04日8mengfanrong

我有一个名为 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。

请您参考如下方法:

您可以只使用 shutil.copy() 命令:

例如

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

[来自 Python 3.6.1 文档。我试过了,它有效。]