简体   繁体   English

Python将文件复制到文件夹

[英]Python copy files to folder

I'm currently trying to copy files from one folder to another (without knowing the names of the files) 我正在尝试将文件从一个文件夹复制到另一个文件夹(不知道文件的名称)

However, it's not working and I can't seem to understand why. 但是,它不起作用,我似乎无法理解为什么。 Below is the code and the error code: 下面是代码和错误代码:

#!/usr/bin/python
import sys, os, time, shutil

path = '/home/images/'
files = os.listdir(path)
files.sort()
for f in files:
        src = path+f
        dst = '/USB/images/' +f
        shutil.move(src, dst)

And the error: 而错误:

Traceback (most recent call last):
  File "copy.py", line 10, in <module>
    shutil.move(dst, src)
  File "/usr/lib/python2.7/shutil.py", line 301, in move
    copy2(src, real_dst)
  File "/usr/lib/python2.7/shutil.py", line 130, in copy2
    copyfile(src, dst)
  File "/usr/lib/python2.7/shutil.py", line 82, in copyfile
    with open(src, 'rb') as fsrc:
IOError: [Errno 2] No such file or directory: '/USB/images/26-07-2015-18:06:22-01.jpg'

Can anybody help me in the right direction? 任何人都可以帮助我朝正确的方向发展吗? Thanks! 谢谢!

The code and error message don't appear to tally up to one another. 代码和错误消息似乎并不相互吻合。

The code suggests you're calling 代码暗示你正在打电话

shutil.move(src, dst)

but the error suggests you're calling 但错误表明你正在打电话

shutil.move(dst, src)

If you're doing the latter then clearly the error message makes sense if the /USB/images/26-07-2015-18:06:22-01.jpg does not already exist. 如果您正在执行后者,那么如果/USB/images/26-07-2015-18:06:22-01.jpg尚不存在,则显然错误消息是有意义的。

You may also have trouble using : characters in the filenames. 您可能还无法使用:文件名中的字符。 The FAT (or derivative) filesystem is common on (typically smaller) USB devices. FAT(或派生)文件系统在(通常较小的)USB设备上是常见的。 That filesystem type does not permit any of the following characters in filenames: "/\\*?<>|: . 该文件系统类型不允许文件名中的以下任何字符: "/\\*?<>|: .

它应该是:

shutil.move(dst, src)

Looks like your destination dir is not writable, or non-existent? 看起来您的目的地目录不可写或不存在? What do you see when you do ls -l /USB/images ? 当你做ls -l /USB/images时你看到了什么?

BTW, I thought you wanted to copy? 顺便说一句,我以为你想复制? shutil.move will move the file, not copy. shutil.move将移动文件,而不是复制。

EDIT: destination VFAT requires special file conversion 编辑:目标VFAT需要特殊的文件转换

How about this: 这个怎么样:

#!/usr/bin/python
import sys, os, time, shutil

path = '/home/images/'
files = os.listdir(path)
files.sort()
for f in files:
    f_dst = f.replace(':','_')
    src os.path.join(path, f)
    dst = os.path.join('/USB/images/', f_dst)
    shutil.move(src, dst)

Use the OS-agnostic function os.path.join() to effectively concatenate file names to folder paths. 使用OS无关的函数os.path.join()有效地将文件名连接到文件夹路径。

#!/usr/bin/python
import sys, os, time, shutil

path = '/home/images/'

files = os.listdir(path)
files.sort()
for f in files:
  src = os.path.join(path, f)
  dst = os.path.join('/USB/images/', f)
  shutil.move(src, dst)

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

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