Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.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 如何将glob与跳过列表一起使用_Python_Glob - Fatal编程技术网

Python 如何将glob与跳过列表一起使用

Python 如何将glob与跳过列表一起使用,python,glob,Python,Glob,我正在尝试使用glob.glob读取一系列目录中具有相同名称的文件,但我想跳过一些特定目录。 我的目录名类似于trj0001、…、trj0099,我想跳过它们的列表,如:list=[trj0005、trj0009、trj0011、trj0056、trj0083] 我目前正在使用这一行: files = glob.glob(r'my_dir/trj_00*/file.txt') 欢迎提供任何提示。在使用glob时,没有简单的方法可以跳过某些文件,除非您希望匹配除与某些模式匹配的文件之外的

我正在尝试使用glob.glob读取一系列目录中具有相同名称的文件,但我想跳过一些特定目录。 我的目录名类似于trj0001、…、trj0099,我想跳过它们的列表,如:list=[trj0005、trj0009、trj0011、trj0056、trj0083]

我目前正在使用这一行:

    files = glob.glob(r'my_dir/trj_00*/file.txt')

欢迎提供任何提示。

在使用
glob
时,没有简单的方法可以跳过某些文件,除非您希望匹配除与某些模式匹配的文件之外的所有文件

因此,您可以使用
filter

exclude_list = [trj0005, trj0009, trj0011, trj0056, trj0083]
files = filter(lambda path: not any(e in path for e in exclude_list), glob.glob(r'my_dir/trj_00*'))

我建议您在之后使用glob过滤列表,使用可读性很强的列表理解,如下所示:

import glob

files = glob.glob(r'my_dir/trj_00*/file.txt')
blackList = ['trj0005', 'trj0009', 'trj0011', 'trj0056', 'trj0083']
files = [f for f in files if all(bl not in f for bl in blackList)]

print(files)

s=set(跳过文件列表);[如果文件不在s中,则在文件中为文件创建文件]
谢谢!放一个跳过目录的列表怎么样?(我编辑了我的问题)@Raha。更新了我的答案谢谢!放一个跳过目录的列表怎么样?(我编辑了我的问题)太好了。谢谢