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

前端 未结 2 384
谎友^
谎友^ 2020-12-14 23:43

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

I have json files named numbers from 50 to 20000 (e.g. 50.json,51.json,52.json...19999.json,20000.json) within the

2条回答
  •  既然无缘
    2020-12-14 23:50

    Although it hardly counts as beautiful code, you could implement your own filtering as follows:

    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
    

    The crucial line here (should) iterate through each file name in the directory (for this_file in all_files), pull out a list of number segments in that file name (re.findall('\d+', this_file)), and include it in read_files if the last of these number segments, as an integer, is greater than 18000.

    I think this will break on files with no integers in the name, so user beware.


    Edit: I see the previous answer has been edited to include what looks a much better thought out way to do this.

提交回复
热议问题