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 ?
Using
os.walk
and generator expression to get all file paths in current directory recursively: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 directoryp
and file namef
.