extract lines from sub-folders using os.walk() module in Python?

402 views Asked by At

I want to open a series of sub-folders in a folder and find some text files and print some lines of the text files. I am using this:

from glob import glob
import fileinput
with open('output.txt', 'w') as out:
    for line in fileinput.input(glob('*.')):
        if 'Subject:' in line:
            out.write(line)

This working perfectly only with in one folder but this cannot access the sub-folders as well.so i heard about the os.walk() module. Does anyone know how I can use the os.walk() module to to access sub-folders and extract and paste a particular line in a separate txt file ?

1

There are 1 answers

0
falsetru On BEST ANSWER

Using os.walk and generator expression to get all file paths in current directory recursively:

from glob import glob
import fileinput
import os

with open('output.txt', 'w') as out:
    files = (os.path.join(p, f) for p, ds, fs in os.walk(os.curdir) for f in fs)
    for line in fileinput.input(files):
        if 'Subject:' in line:
            out.write(line)

fs in the above code is a list of file names. You need to iterate them to get file paths.

os.path.join is used to make a path by joining parent directory p and file name f.