How to use glob to read a limited set of files with numerical names?

How to use glob to read only a limited set of files?

I have json files with numbers from 50 to 20,000 (e.g. 50.json, 51.json, 52.json ... 19999.json, 20000.json) in the same directory. I want to read only files with numbers from 15000 to 18000.

For this, I use glob as shown below, but it generates an empty list every time I try to filter out the numbers. I 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
[]

Also, what if I need files with any number greater than 18000?

+4
source share
2

glob ; [..] . glob :

'1[5-8][0-9][0-9][0-9].*'

glob fnmatch, . :

>>> import fnmatch
>>> fnmatch.translate('[15000-18000].*')
'[15000-18000]\\..*\\Z(?ms)'

1 ., a 0, 1, 5 8. .

glob ; ; , (glob('1[8-9][0-9][0-9][0-9]') + glob('2[0-9][0-9][0-9][0-9]') ..).

:

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)
+8

, :

import os, re
directory = "/Users/Chris/Dropbox"
all_files = os.listdir(directory)

read_files = [this_file for this_file in all_files 
                if (int(re.findall('\d+', this_file)[-1]) > 18000)]

print read_files

() (for this_file in all_files), (re.findall('\d+', this_file)) read_files, , , 18000.

, , .


: , , .

+2

Source: https://habr.com/ru/post/1541101/


All Articles