Created
July 4, 2019 11:28
-
-
Save potaty/1de9fdd2b70b934c04777f014b0acb41 to your computer and use it in GitHub Desktop.
traverse files under one folder recursively
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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