The Python Oracle

Read all files in directory and subdirectories in Python

--------------------------------------------------
Rise to the top 3% as a developer or hire one of them at Toptal: https://topt.al/25cXVn
--------------------------------------------------

Music by Eric Matyas
https://www.soundimage.org
Track title: Over Ancient Waters Looping

--

Chapters
00:00 Read All Files In Directory And Subdirectories In Python
01:02 Accepted Answer Score 40
01:20 Thank you

--

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

--

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: