Skip to content Skip to sidebar Skip to footer

Python Read All Files In Directory And Subdirectories

I'm trying to translate this bash line in python: find /usr/share/applications/ -name '*.desktop' -exec grep -il 'player' {} \; | sort | while IFS=$'\n' read APPLI ; do grep -ilqw

Solution 1:

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:

Post a Comment for "Python Read All Files In Directory And Subdirectories"