Python: How do I iterate over multiple folders to open a text file (all named the same), extract data from specific lines (floats added up), and print

Viewed 169

This is my code so far. I am using a root directory called Start_Path, and I am trying to open every file in there called summary.txt. Then once opened, search only for lines that have the word, 'Slab', and add all numbers up in each slab line (multiple lines per text file), and tell me the individual text file numbers. Like one text file has the slab total number of 45.58 as seen in the bottom output example. I've got 18 files, so the full output is just really long. I am not sure how to get the final sum value of each summary file (such as 45.58) as its own variable.

    File = 'summary.txt'
    Start_Path = 'C://Users//me//Documents//Project//'
    for root, dirs, files in os.walk(Start_Path):
        if File in files and root.split(os.path.sep)[1] == "report":
            summary_path = root + os.path.sep + File
            search_file = open(summary_path, 'r', encoding='utf-8')
            slab_count = 0
            slab_number = []
            slab_total = 0
            for line in search_file:
                if "Slab" in line:
                    slab_num = ([float(s) for s in re.findall(r'[-+]?(?:\d*\.\d+|\d+)', line)])
                    slab_percent = slab_num[-1]
                    slab_number.append(slab_percent)
                    slab_count = slab_count + 1
                    slab_total=0
                    for slab_percent in slab_number:
                        slab_total += slab_percent
                    print('Line number:', slab_count)
                    print('Slab percentages:', slab_number)
                    print('Slab percentage total:',slab_total)

Output is this:

Line number: 1
Slab percentages: [26.36]
Slab percentage total: 26.36
Line number: 2
Slab percentages: [26.36, 11.84]
Slab percentage total: 38.2
Line number: 3
Slab percentages: [26.36, 11.84, 3.04]
Slab percentage total: 41.24
Line number: 4
Slab percentages: [26.36, 11.84, 3.04, 1.23]
Slab percentage total: 42.47
Line number: 5
Slab percentages: [26.36, 11.84, 3.04, 1.23, 1.15]
Slab percentage total: 43.62
Line number: 6
Slab percentages: [26.36, 11.84, 3.04, 1.23, 1.15, 1.01]
Slab percentage total: 44.629999999999995
Line number: 7
Slab percentages: [26.36, 11.84, 3.04, 1.23, 1.15, 1.01, 0.39]
Slab percentage total: 45.019999999999996
Line number: 8
Slab percentages: [26.36, 11.84, 3.04, 1.23, 1.15, 1.01, 0.39, 0.36]
Slab percentage total: 45.379999999999995
Line number: 9
Slab percentages: [26.36, 11.84, 3.04, 1.23, 1.15, 1.01, 0.39, 0.36, 0.2]
Slab percentage total: 45.58
1 Answers

I cannot test the following answer because of not having the data.

See if it merges with the original code to accomplish the objectives.

File = 'summary.txt'
Start_Path = 'C://Users//me//Documents//Project//'

# Add a data structure to capture the file name and total. It could be a
# dictionary or a list
# Example a list of tuples
file_with_totals = [] # will return a list of tuple (filename,total)

for root, dirs, files in os.walk(Start_Path):
    if File in files and root.split(os.path.sep)[1] == "report":
        summary_path = root + os.path.sep + File
        search_file = open(summary_path, 'r', encoding='utf-8')
        slab_count = 0
        slab_number = []
        slab_total = 0
        for line in search_file:
            if "Slab" in line:
                slab_num = (
                    [float(s) for s in
                     re.findall(r'[-+]?(?:\d*\.\d+|\d+)', line)])
                slab_percent = slab_num[-1]
                slab_number.append(slab_percent)
                slab_count = slab_count + 1
                slab_total = 0
                for slab_percent in slab_number:
                    slab_total += slab_percent
                print('Line number:', slab_count)
                print('Slab percentages:', slab_number)
                print('Slab percentage total:', slab_total)
        # Accumulate fine name and total for the current filename.
        file_with_totals.append((summary_path, slab_total))

print(file_with_totals) # Display accumulated results

with open('summary.txt','w') as f_out:
    for item in file_with_totals:
        filename = item[0]
        total = item[1]
        f_out.write(f'{filename},{total}')

Related