ISEexchange / Python-Bootcamp

Discussion/Materials regarding the Python Bootcamp class
3 stars 7 forks source link

Subprocess Calls (executing command lines) #5

Open jwasserzug opened 10 years ago

jwasserzug commented 10 years ago

https://docs.python.org/2/library/subprocess.html

http://en.wikipedia.org/wiki/Here_document

import subprocess

subprocess_call = subprocess.Popen(args='ls -ltr', stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)

# Using communicate() will suspend the script until the subprocess is complete
output = subprocess_call.communicate() 
stdout = output[0]
stderr = output[1]

heredoc  = "<<SCRIPT\n"
heredoc += "cd /home/wassjas\n"
heredoc += "ls -ltr\n"
heredoc += "pwd\n"
heredoc += "SCRIPT"

heredoc_call = subprocess.Popen(args=heredoc, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=True)

# Communicate blocks until subprocess call finishes
heredoc_out    = heredoc_call.communicate()
heredoc_stdout = heredoc_out[0]
heredoc_stderr = heredoc_out[1]

@vraghunandan @bdambola @kwokman @dsapienza @DRozentsvay @gganeshan @cbautista1002 @xingj53 @wcorrea @rklotz @isehgu @akbamaa