Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

I have a directory(data) that contain thousand of files.Each time I want to select three files that are just differ by only one characterAB[C,D,E] and want to perform some computation on the selected three files later.

My files are present inside the directory as follows

DT.ABC.2007.182.144018.txt
DT.ABD.2007.182.144018.txt
DT.ABE.2007.182.144018.txt

DT.ABC.2001.005.1444.txt
DT.ABD.2001.005.1444.txt
DT.ABE.2001.005.1444.txt

DT.ABC.2003.005.1244.txt
DT.ABD.2003.005.1244.txt
DT.ABE.2003.005.1244.txt

and at first i want to print

    DT.ABC.2007.182.144018.txt
    DT.ABD.2007.182.144018.txt
    DT.ABE.2007.182.144018.txt

then

DT.ABC.2001.005.1444.txt
DT.ABD.2001.005.1444.txt
DT.ABE.2001.005.1444.txt

and same process would goes on until finishing reading all the files in the directory.

I tried the code below:

import glob
for file in glob.glob('/data/*.txt'):
    print(st)

But it print all the files randomly instead of printing the same three(differ only by [C,D,E] character.I hope experts may help me.Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
148 views
Welcome To Ask or Share your Answers For Others

1 Answer

Here is a simple function which lists files and groups them by the first and third component of the file name.

def groupfiles(pattern):
    files = glob.glob(pattern)
    filedict = defaultdict(list)
    for file in files:
        parts = file.split(".")
        filedict[".".join([parts[0], parts[2]])].append(file)
    for filegroup in filedict.values():
        yield filegroup

This groups together and returns a list of files at a time (yield is a keyword which produces a generator; but you can think of it as a sort of replacement for return, only the function continues where it left off after the previous call instead of running from the start the next time you call it) and so does not hard-code the limit of three files at a time.

Demo: https://ideone.com/w2Sf80


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...