Click name ↑ to return to homepage
import subprocess
ls
, pass in the list of one item:subprocess.run(['ls'])
cmd=['ls']
subprocess.run(cmd)
stdout=subprocess.PIPE
proc1
with proc1.stdout
cmd=['ls']
proc1 = subprocess.run(cmd, stdout=subprocess.PIPE)
print(proc1.stdout)
f = open("output.txt", "w") # this creates the file
cmd=['ls']
subprocess.run(cmd, stdout=f) # this sends it to f
output.txt
less output.txt
f
:with open("output.txt", "r") as f:
print(f.read())
Pipe in to process 2
cmd1 = ['ls'] #list my files
cmd2 = ['wc', '-l'] #count number of lines
Note that in cmd2, the arguments are not wc -l
but wc
and -l
Make a file to capture the output
f = open("numlines.txt", "w")
proc1 = subprocess.run(cmd1, stdout=subprocess.PIPE)
proc2 = subprocess.run(cmd2, input=proc1.stdout, stdout=f)
import os
os.system('echo this is os system') # runs the echo comnmand
os.system("bash please.sh") # runs please.sh with bash
os.system("python chips.py") # runs chips.py with python