python - 在 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。
解决方案
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.]
推荐阅读
- node.js - Node.js 多线程和异步
- swift - 更改视图宽度约束
- php - 在 HTTP 请求中显式设置会话 ID 是否安全?
- audiokit - 如何在 AudioKit 中将音符翻译成 MIDINoteNumber?
- prolog - 创建一个谓词,该谓词将在调用时为查询编写解决方案
- docker - 使用 Traefik 反向代理的 Docker 设置 - NET:ERR_CERT_AUTHORITY_INVALID 在一个域上,尽管另一个域的设置和配置相同
- powershell - Powershell G-cloud 将磁盘附加到实例
- java - 从 xml 应用程序上下文 Spring 加载命令行参数
- android - Firestore 快照侦听器作为服务?
- google-cloud-platform - 轮换密钥和反应式再加密数据