简体   繁体   中英

I want to use python to walk through directories to get to text files and processed them

I have many folders in a directory:

/home/me/Documents/coverage

/coverage contains 50 folders all beginning with H :

/home/me/Documents/coverage/H1   (etc)

In each H*** folder there is a text file which I need to extract data from.

I have been trying to use glob and os.walk to use a script that is saved in /coverage to walk into each of these H folders, open the .txt file and process it, but I have had no luck at all.

Would this be a good starting point? (where path = /coverage )

for filename in glob.glob(os.path.join(path, "H*")):
    folder = open(glob.glob(H*))

And then try and open the .txt file?

Just gather all the txt files in one shot using glob wildcards. You can do it like that.

import glob
path = "/home/me/Documents/coverage/H*/*.txt"
for filename in glob.glob(path):
    fileStream = open(filename )

cheers

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM