Skip to content

Subprocess Calls (executing command lines) #5

@jwasserzug

Description

@jwasserzug

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions