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

标签 python python-3.x file-copying

我有一个名为 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 文档。我试过了,它有效。]

关于python - 在 Python 中将文件从一个位置复制到另一个位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52851994/

相关文章:

python - 如何在mongodb聚合中对嵌套字段进行分组

python-3.x - 打印各种组合中的第一个组合

python - 在 LIST Python 中找到第一个以元音开头的字符串

Python 无法创建文件错误 [错误 : 183]

c# - 提高分割文件的速度

python - 在Python中转义单个 '{'

python - 在 Windows 上出现 UnicodeDecodeError,但在 Mac 上运行完全相同的代码时不会出现

python - 在python中使用日期获取周数

python - 如何将输入压缩到 txt 文件中?

java - 在Java中将文件从一个文件夹复制到另一个文件夹