Skip to content

Instantly share code, notes, and snippets.

@keithweaver
Created October 3, 2017 11:33
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/f77c5001e6e38b7c8fa5a4fb1505bc73 to your computer and use it in GitHub Desktop.
Save keithweaver/f77c5001e6e38b7c8fa5a4fb1505bc73 to your computer and use it in GitHub Desktop.
Running the "ls" (list) command in Python
# Demo of using the ls command in Python
# https://stackoverflow.com/questions/4514751/pipe-subprocess-standard-output-to-a-variable
import subprocess
proc = subprocess.Popen('ls', stdout=subprocess.PIPE)
output = proc.stdout.read()
print output
# My terminal:
# > $ python ls-example.py
# > README.md
# > ls-example.py
# >
# This is the output this script returned. The folder contained README.md,
# ls-example.py and .gitignore file. It also contained a .git folder. I don't
# know what the extra blank one is between .git and .gitignore cause I added
# a sub folder "test" and I got that in the list when I ran this again.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment