Skip to content

Instantly share code, notes, and snippets.

@mohi7solanki
Created March 25, 2018 18:07
Show Gist options
  • Save mohi7solanki/d61df40a7c0a03b630a196feac08f25f to your computer and use it in GitHub Desktop.
Save mohi7solanki/d61df40a7c0a03b630a196feac08f25f to your computer and use it in GitHub Desktop.
Get all the files in a directory.
def get_all_files(BASE_PATH, no_subdir):
"""
Return a generator that yields all the files in the
directory you pass, if no_subdir is True it won't look
for files in sub-directories.
"""
def with_subdir():
for root, _, files in os.walk(BASE_PATH):
for file in files:
file_path = os.path.join(root, file)
if not os.path.islink(file_path):
yield file_path
def without_subdir():
for file in os.listdir(BASE_PATH):
file_path = os.path.join(BASE_PATH, file)
if os.path.isfile(file_path) and not os.path.islink(file_path):
yield file_path
if no_subdir:
return without_subdir()
return with_subdir()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment