Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在文本文件中指定的目录上运行函数的Python命令_Python_Command Line_Directory - Fatal编程技术网

在文本文件中指定的目录上运行函数的Python命令

在文本文件中指定的目录上运行函数的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

我有一个名为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.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