The Python Oracle

Read all files in directory and subdirectories in Python

Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn

--

Music by Eric Matyas
https://www.soundimage.org
Track title: Puzzle Game Looping

--

Chapters
00:00 Question
01:22 Accepted answer (Score 35)
01:47 Thank you

--

Full question
https://stackoverflow.com/questions/2586...

Accepted answer links:
[https://docs.python.org/2/library/os.htm...]: https://docs.python.org/2/library/os.htm...

--

Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...

--

Tags
#python #file #subdirectory

#avk47



ACCEPTED ANSWER

Score 40


Looks like you are doing os.walk() loop incorrectly. There is no need for nested dir loop.

Please refer to Python manual for the correct example:

https://docs.python.org/2/library/os.html?highlight=walk#os.walk

for root, dirs, files in os.walk('python/Lib/email'):
     for file in files:
        with open(os.path.join(root, file), "r") as auto: