Skip to content

Instantly share code, notes, and snippets.

@PttCodingMan
Created November 10, 2020 03:40
Show Gist options
  • Save PttCodingMan/fcc045475d60d53c08f53df5fa0a13d3 to your computer and use it in GitHub Desktop.
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
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