Skip to content

Instantly share code, notes, and snippets.

@keithweaver
Created October 3, 2017 11:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save keithweaver/2d6a8552a4d11fe889a7f84bc81eea02 to your computer and use it in GitHub Desktop.
Save keithweaver/2d6a8552a4d11fe889a7f84bc81eea02 to your computer and use it in GitHub Desktop.
Running the "ls" (list) command in Python for a specific directory
# Demo of using the ls command in Python
# I have another one here: https://gist.github.com/keithweaver/f77c5001e6e38b7c8fa5a4fb1505bc73
# But I really didn't want it to be the current path. I want to pass the path
# in. I found this example of listing all files in a path from SO:
# https://stackoverflow.com/questions/3207219/how-do-i-list-all-files-of-a-directory
# Works perfect.
from os import listdir
from os.path import isfile, join
mypath = '../';
onlyfiles = [f for f in listdir(mypath) if isfile(join(mypath, f))]
print onlyfiles
# Returns a list of strings.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment