Skip to content

Instantly share code, notes, and snippets.

@potaty
Created July 4, 2019 11:28
Show Gist options
  • Save potaty/1de9fdd2b70b934c04777f014b0acb41 to your computer and use it in GitHub Desktop.
Save potaty/1de9fdd2b70b934c04777f014b0acb41 to your computer and use it in GitHub Desktop.
traverse files under one folder recursively
#!/usr/bin/python
import os
def traverse_dir_recur(dir):
import os
l = os.listdir(dir)
for d in l:
if os.path.isdir(dir + '/' + d):
traverse_dir_recur(dir + '/' + d + "/")
else:
f = open(dir + '/' + d, 'r')
try:
line = f.read()
# do something
# bytes(line, 'utf-8').decode('utf-8')
except UnicodeDecodeError:
print(dir + '/' + d)
traverse_dir_recur('/path/to/folder')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment