I have about 100 folders with random names, say for this example 1,2,3,4,...100. Inside these folders, I have text files with some strings in them. eg: sample.txt.
The text files all have the same name but are in different folders. What I need is to read the files from inside these folders, and read the text inside these files and print out or save the location of these text files.
I only know how to read lines from a file if it is in my pwd and look for stuff in it. I use the following code for that:
with open(r'Example.txt', 'r') as infile_txt:
for line in infile_txt:
if r"sample" in line:
print line
How can I read files from inside folders and record the names of these folders?
You can use os for that. For example:
This will give you all subfolders and files in a list. You can then traverse the list to find needed subfolder and repeat the action.