Created
November 10, 2020 03:40
-
-
Save PttCodingMan/fcc045475d60d53c08f53df5fa0a13d3 to your computer and use it in GitHub Desktop.
List all files in the folder in Python 3, recursion is an option
This file contains 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 | |
if __name__ == '__main__': | |
def walk_dir(current_path, recursive=True): | |
result = [] | |
for (parent_path, folder_names, file_names) in os.walk(current_path): | |
# I don't like -> \ <- | |
parent_path = parent_path.replace('\\', '/') | |
if not recursive: | |
break | |
for file_name in file_names: | |
current_result = f'{parent_path}/{file_name}' | |
result.append(current_result) | |
return result | |
result = walk_dir(r'/path/to/folder') | |
for file in result: | |
print(file) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment