Last active
March 15, 2017 16:05
-
-
Save slv922/553f048a8ca82833f6105ef7e5513989 to your computer and use it in GitHub Desktop.
Recurcive read files from path
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
| import os | |
| import sys | |
| walk_dir = sys.argv[1] | |
| print('walk_dir = ' + walk_dir) | |
| # If your current working directory may change during script execution, it's recommended to | |
| # immediately convert program arguments to an absolute path. Then the variable root below will | |
| # be an absolute path as well. Example: | |
| # walk_dir = os.path.abspath(walk_dir) | |
| print('walk_dir (absolute) = ' + os.path.abspath(walk_dir)) | |
| for root, subdirs, files in os.walk(walk_dir): | |
| print('--\nroot = ' + root) | |
| list_file_path = os.path.join(root, 'my-directory-list.txt') | |
| print('list_file_path = ' + list_file_path) | |
| with open(list_file_path, 'wb') as list_file: | |
| for subdir in subdirs: | |
| print('\t- subdirectory ' + subdir) | |
| for filename in files: | |
| file_path = os.path.join(root, filename) | |
| print('\t- file %s (full path: %s)' % (filename, file_path)) | |
| with open(file_path, 'rb') as f: | |
| f_content = f.read() | |
| list_file.write(('The file %s contains:\n' % filename).encode('utf-8')) | |
| list_file.write(f_content) | |
| list_file.write(b'\n') |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
http://stackoverflow.com/questions/2212643/python-recursive-folder-read