0
我有一个名为“folders”的文本文件,并在其中列出了我想要运行功能的目录(可以包含通配符)。Python命令在文本文件中指定的目录上运行功能
folders.txt:
Images*\ //Any folders that start with Images)
Music\Rap //The specific folder Music\Rap)
Video\Horror //The specific folder Video\Horror)
Python代码:
directories = folders.readlines()
for lines in directories:
lines = lines.strip()
command= 'dir ' + RootPath + '\\' + lines + ' /B'
result = subprocess.check_output(command, shell=True)
for line in result.split('\r\n'):
Execute function
我决定使用dir命令的原因,这是我知道如何处理通配符的唯一途径。但运行“dir c:\ Music \ Rap/B”会列出该目录内的所有文件,而不是目录本身。有一个更好的方法吗?
使用'glob',它将返回匹配 – Hackaholic 2014-11-03 23:28:36
使用'os.listdir'或'os.walk'如果你需要搜索子目录 – 2014-11-03 23:45:54