在文本文件中指定的目录上运行函数的Python命令
我有一个名为folders的文本文件,其中列出了我想要运行函数的目录,其中可以包含通配符 folders.txt:在文本文件中指定的目录上运行函数的Python命令,python,command-line,directory,Python,Command Line,Directory,我有一个名为folders的文本文件,其中列出了我想要运行函数的目录,其中可以包含通配符 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.read
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命令,因为这是我知道如何处理通配符的唯一方法。但是运行dirc:\Music\Rap/B会列出该目录中的所有文件,而不是目录本身。有更好的方法吗?glob模块包含根据文件系统内容扩展通配符的方法。例如:
import glob
result = glob.glob( 'Images*' )
使用glob,如果需要搜索子目录,它将返回matchuse os.listdir或os.walk