如何使用glob读取具有数字名称的有限文件集? [英] How to use glob to read limited set of files with numeric names?
问题描述
如何使用glob仅读取有限的文件集?
How to use glob to only read limited set of files?
我在同一目录中有json文件,它们的编号从50到20000(例如50.json,51.json,52.json ... 19999.json,20000.json).我只想读取编号从15000到18000的文件.
I have json files named numbers from 50 to 20000 (e.g. 50.json,51.json,52.json...19999.json,20000.json) within the same directory. I want to read only the files numbered from 15000 to 18000.
为此,我使用了一个glob,如下所示,但是每次我尝试过滤出数字时,它都会生成一个空列表.我已尽力遵循此链接( https://docs.python.org/2/library/glob.html ),但我不确定自己做错了什么.
To do so I'm using a glob, as shown below, but it generates an empty list every time I try to filter out for the numbers. I've tried my best to follow this link (https://docs.python.org/2/library/glob.html), but I'm not sure what I'm doing wrong.
>>> directory = "/Users/Chris/Dropbox"
>>> read_files = glob.glob(directory+"/[15000-18000].*")
>>> print read_files
[]
另外,如果我想要大于18000的文件怎么办?
Also, what if I wanted files with any number greater than 18000?
推荐答案
您错误地使用了glob语法; [..]
序列对每个字符有效 .以下glob将改为正确匹配您的文件:
You are using the glob syntax incorrectly; the [..]
sequence works per character. The following glob would match your files correctly instead:
'1[5-8][0-9][0-9][0-9].*'
在幕后,glob
使用fnmatch
将模式转换为正则表达式.您的模式将转换为:
Under the covers, glob
uses fnmatch
which translates the pattern to a regular expression. Your pattern translates to:
>>> import fnmatch
>>> fnmatch.translate('[15000-18000].*')
'[15000-18000]\\..*\\Z(?ms)'
与.
,0
,1
,5
或8
前面的 1 字符匹配的
.没什么.
which matches 1 character before the .
, a 0
, 1
, 5
or 8
. Nothing else.
glob
模式非常有限;匹配数字范围并不容易;您必须为范围(例如glob('1[8-9][0-9][0-9][0-9]') + glob('2[0-9][0-9][0-9][0-9]')
等)创建单独的全局文件.
glob
patterns are quite limited; matching numeric ranges is not easy with it; you'd have to create separate globs for ranges, for example (glob('1[8-9][0-9][0-9][0-9]') + glob('2[0-9][0-9][0-9][0-9]')
, etc.).
请自行过滤:
directory = "/Users/Chris/Dropbox"
for filename in os.listdir(directory):
basename, ext = os.path.splitext(filename)
if ext != '.json':
continue
try:
number = int(basename)
except ValueError:
continue # not numeric
if 18000 <= number <= 19000:
# process file
filename = os.path.join(directory, filename)
这篇关于如何使用glob读取具有数字名称的有限文件集?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!