Created
October 3, 2017 11:39
-
-
Save keithweaver/2d6a8552a4d11fe889a7f84bc81eea02 to your computer and use it in GitHub Desktop.
Running the "ls" (list) command in Python for a specific directory
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
# 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