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

How to move multiple files whith spaces in their names with python?

我想将多个文件(仅文件而不是文件夹)从目录 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

如何为多个文件打点?

source 定义的路径部分将包含在 files 中定义的文件路径中。在循环中将 source 添加到 f 会产生冗余。而是尝试:

shutil.move(f, dest)

此外,我不确定您为什么要添加 "r'"。也许您的意思是将源定义为原始输入,例如您定义 file_1 时?在那种情况下你应该执行这样的事情:

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