Last active
October 11, 2018 14:26
-
-
Save cgpu/8c8b19aa11a84d66bce41e8cfbc2066a to your computer and use it in GitHub Desktop.
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
| # Python-script header etiquette: | |
| # https://stackoverflow.com/questions/1523427/what-is-the-common-header-format-of-python-files | |
| #!/usr/bin/env python3 | |
| # -*- coding: utf-8 -*- | |
| """ | |
| @author : cristina | |
| LANG : Python # [i] print(platform.python_version()) [o]: 3.6.3 | |
| WHAT : Script for parsing all .txt or .csv etc files of a user-defined input directory | |
| INPUT : User defined arguments, `input_dir_absolut_path` | |
| TASK : Iterates over all files in a given directory | |
| RETURNS : A list with the full paths of the .txt files detected in the | |
| HOW TO : from directoryFilenameGrabber import filepathGrabber | |
| list_of_filepaths_in_my_directory = filepathGrabber(my_input_directory) | |
| SOLUTION : @stack: https://stackoverflow.com/questions/10377998/how-can-i-iterate-over-files-in-a-given-directory | |
| """ | |
| def filepathGrabber(input_dir_absolut_path, file_extension): | |
| # Ze imports | |
| from pathlib import Path | |
| # the Path call is a generator and fetches the filepaths on the fly | |
| # To store the filepaths as strings, call the generator object `pathlist` | |
| pathlist = Path(input_dir_absolut_path).glob('**/*' + str(file_extension)) | |
| # Extract filepath strings from generator object `pathlist` | |
| directory_files_list = [str(path) for path in pathlist] | |
| # Addition for `Windows` shenanigans with filepaths | |
| directory_files_list = [ path.replace("\\", "/") for path in directory_files_list] | |
| return(directory_files_list) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment