在nam中使用空格移动文件

2024-10-01 19:31:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图在python中移动一些文件,但是它们的名称中有空格。有没有什么方法可以明确地告诉python将字符串作为文件名来处理?在

listing = os.listdir(self.Parent.userTempFolderPath)
for infile in listing:
    if infile.find("Thumbs.db") == -1 and infile.find("DS") == -1:

        fileMover.moveFile(infile, self.Parent.userTempFolderPath, self.Parent.currentProjectObject.Watchfolder, True)

在我从清单中得到文件后,我对它运行os.path.exists,看看它是否存在,它永远不存在!有人能给我个提示吗?在


Tags: 文件方法字符串self名称foros文件名
1条回答
网友
1楼 · 发布于 2024-10-01 19:31:30

文件名中的空格不是问题;os.listdir返回文件名,而不是完整路径。在

您需要将它们添加到您的文件名中以测试它们;^{}将使用适合您平台的正确目录分隔符为您执行此操作:

listing = os.listdir(self.Parent.userTempFolderPath)
for infile in listing:
    if 'Thumbs.db' not in infile and 'DS' not in infile:
        path = os.path.join(self.Parent.userTempFolderPath, infile)

        fileMover.moveFile(path, self.Parent.userTempFolderPath, self.Parent.currentProjectObject.Watchfolder, True)

注意,我还简化了文件名测试;我没有使用.find(..) == -1,而是使用了not in运算符。在

相关问题 更多 >

    热门问题