Created
October 3, 2017 11:33
-
-
Save keithweaver/f77c5001e6e38b7c8fa5a4fb1505bc73 to your computer and use it in GitHub Desktop.
Running the "ls" (list) command in Python
This file contains 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 | |
# 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