Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - loop through files in different folders

I want to extract data from multiple files nested within subfolders.

e.g. folder structure

A/B/C/D.dat
A/B/E/F.dat
A/B/G/H.dat

The code I came up with is:

import os
values = 2
doc = []
rootdir = 'C:/A/B'

for subdir, dirs, files in os.walk(rootdir):
    for file in files:
        if file.endswith('.dat'):
            with open (file, 'rt') as myfile:
                    current_line = 0
                    for mylines in myfile:
                            if current_line == values:
                                doc.append()
                                break
                            current_line += 1
            continue

print(doc)

Error I struggle to solve:

...with open (file, 'rt') as myfile:
IOError: [Errno 2] No such file or directory: 'D.dat'
like image 979
Matt Avatar asked Apr 12 '26 22:04

Matt


1 Answers

Though your solution is not the cleanest. The bug you are getting comes from

            with open (file, 'rt') as myfile:

which should be replaced with

            with open (subdir + "/" + file, 'rt') as myfile:
like image 105
Philip Lassen Avatar answered Apr 14 '26 23:04

Philip Lassen



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!