Search code examples
pythondirectoryglobos.walk

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?


Solution

  • 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