Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
161 views
in Technique[技术] by (71.8m points)

python - printing files based on character

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

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

1 Reply

0 votes
by (71.8m points)

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...