简体   繁体   English

尝试以Python脚本复制文件,但不起作用

[英]Trying to copy a file in Python script, but it doesn't work

I'm trying to copy a file (image.jpg) from the folder 'src' to the folder 'dst', but I've got an error: 我正在尝试将文件(image.jpg)从文件夹src复制到文件夹dst,但是出现错误:

Traceback (most recent call last): File "exec.py", line 7, in shutil.copyfile(file, destination) File "C:\\Users\\mike\\AppData\\Local\\Programs\\Python\\Python35-32\\lib\\s hutil.py", line 114, in copyfile with open(src, 'rb') as fsrc: FileNotFoundError: [Errno 2] No such file or directory: 'image.jpg' 追溯(最近一次通话):文件“ exec.py”,第7行,位于shutil.copyfile(文件,目标)中,文件“ C:\\ Users \\ mike \\ AppData \\ Local \\ Programs \\ Python \\ Python35-32 \\ lib \\ s hutil.py“,行114,在具有open(src,'rb')作为fsrc的复制文件中FileNotFoundError:[Errno 2]没有这样的文件或目录:'image.jpg'

This is my code: 这是我的代码:

import shutil, os

source = os.listdir('C:/Users/mike/Pictures/src/')
destination = 'C:/Users/mike/Pictures/dst/'

for file in source:
    shutil.copy(file, destination)

Python 3.5 / Windows 7 Python 3.5 / Windows 7

os.listdir returns the names, but they don't have the directory prefix on them, you need to add this when copying. os.listdir返回名称,但是名称上没有目录前缀,复制时需要添加名称。

for file in source:
    shutil.copy(os.path.join('C:/Users/mike/Pictures/src/', file), destination)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM