我正在运行一个脚本,该脚本试图递归地运行一个文件文件夹,并计算给定的文件类型中有多少类型。当文件夹名中没有空格时,它工作得很顺利。为什么会这样呢?解决这个问题的好方法是什么?谢谢!
import os
import fnmatch
def getPath():
path = raw_input("Drag file/enter filepath here: ")
return path
def convert():
patterns = ['*.wav', '*.aif', '*.aiff', '*.flac', '*.alac']
count = 0
for index in patterns:
for root, dirs, files in os.walk(rootPath):
for filename in fnmatch.filter(files, index):
inputName = os.path.join(root, filename)
print inputName
count = count + 1
print count
rootPath = getPath()
convert()发布于 2013-09-12 01:29:23
尝试this other question中的解决方案。定义另一个功能:
def shellquote(s):
return "'" + s.replace("'", "'\\''") + "'"然后,当您在path中捕获getPath()时,请执行
new_path = shellquote(path)并返回new_path而不是path。
当您拖拽文件或写入带有空格的路径时,您需要转义空格或将整个路径放在引号中,这样就可以清楚地看到它是一个字符串。shellquotes()功能实现了这两种功能。
https://stackoverflow.com/questions/18753194
复制相似问题