3

I have 540 files in my directory. All files' data formats are given below:

File input.txt

class    confidence    Xmin        Ymin        Xmax           Ymax
7         0.3456900    89          8            39             53
6         0.0123457    2           1            23             43

File result.txt

class    confidence    Xmin        Ymin        Xmax           Ymax
6         0.0123457    2           1            23             43
7         0.3456900    89          8            39             53

I've solved this problem for reading a single file. Code is given below:

This is my single text file compare code. It's working. But I have 540 text files, and I want to sort files like this. How can I sort multiple files in my directory by the same process ? I need to make a specific file name for each file.

from collections import defaultdict 

maxima = defaultdict(int)

with open('F:\GGR\grnd.txt', 'r') as ifh:
    for line in ifh:
        key, value = line.rsplit(None, 1)
        value = int(value)
        if value > maxima[key]:
            maxima[key] = value

with open('output.txt', 'w') as ofh:
    for key in sorted(maxima):
        ofh.write('{} {}\n'.format(key, maxima[key]))
0

1 Answer 1

1

Use os.walk to find all files under your directory uncluding subdirs:

Your code adapted:

from collections import defaultdict 
import os

for root, dirs, files in os.walk(r'F:\GGR'):  # this recurses into subdirectories as well
    for f in files:
        maxima = defaultdict(int)
        try:
            with open(os.path.join(root,f)) as ifh:
                for line in ifh:
                    key, value = line.rsplit(None, 1)
                    value = int(value)
                    if value > maxima[key]:
                        maxima[key] = value

            with open(os.path.join(root, f'{f}.out'), 'w') as ofh:
                for key in sorted(maxima):
                    ofh.write('{} {}\n'.format(key, maxima[key]))
        except ValueError:
            # if you have other files in your dir, you might get this error because they 
            # do not conform to the structure of your "needed" files - skip those
            print(f, "Error converting value to int:", value)

If you do not need to recurse into subdirs, use os.listdir

A better solution:

Sort the files content directly using the key argument for sorted :

from collections import defaultdict 
import os

for root, dirs, files in os.walk(r'./'):
    for f in files:
        print(f)
        maxima = defaultdict(int)
        try:
            with open(os.path.join(root,f)) as ifh, open(
                      os.path.join(root, f'{f}.out'), 'w') as ofh:
                # header
                ofh.write(next(ifh))  
                # data
                ofh.write( '\n'.join(sorted(ifh.readlines(), key = 
                                            lambda x: int(x.split()[-1])))) 
        except ValueError:
            print(f, "Error converting value to int:", ifh)
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.