Py学习  »  Python

如何用python将多个文件名称中的空格移动?

Ger Cas • 7 年前 • 1729 次点击  

我要从目录中移动多个文件(仅文件而不是文件夹) source 到目录 dest . 我在下面用 for 循环:

import os
import shutil
import glob

source = "r'" + "/This is/the source path/"
dest = "r'" + "/This is/the destination path/"

files = glob.glob(source+'/*.*')    

for f in files:
    shutil.move(source+f, dest)

>> IOError: [Errno 2] No such file or directory:

但是如果我为这样的一个文件做,它是有效的。

source = "/This is/the source path/"
dest = "/This is/the destination path/"

file_1 = r'This is a file.txt'
shutil.move(source+file_1, dest) ## This works

我怎样才能点几个文件?

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/39473
文章 [ 1 ]  |  最新文章 7 年前
busybear
Reply   •   1 楼
busybear    7 年前

由定义的路径部分 source 将包含在您在中定义的文件路径中 files . 添加 来源 到 f 在循环中会产生冗余。相反,请尝试:

shutil.move(f, dest)

另外,我不知道你为什么要添加 "r'" . 也许您的意思是将源定义为原始输入,例如当您定义 file_1 ?在这种情况下,您应该执行如下操作:

 source = r'/some/path/to/file.ext'