user1991858
user1991858

Reputation: 21

How to write a subprocess.Popen line of code

I have used

    self.session.open(MoviePlayer, sref)

to start playing a file with MoviePlayer in my python 2.6 code, I have been advised that i should use

    subprocess.Popen()

but am unsure how I should convert the above line to use this.

Upvotes: 0

Views: 283

Answers (2)

Ali Okan Yüksel
Ali Okan Yüksel

Reputation: 388

There are several ways to run system command with python

import os
os.system("date")

or

import os
f = os.popen('date')
now = f.read()
print "Today is ", now

or if you want to use subprocesss:

import subprocess
subprocess.call("command1")
subprocess.call(["command1", "arg1", "arg2"])

==

import subprocess
subprocess.call(["ls", "-l", "/etc/passwd"])

==

import subprocess
p = subprocess.Popen("date", stdout=subprocess.PIPE, shell=True)
(output, err) = p.communicate()
print "Today is", output

==

import subprocess
p = subprocess.Popen(["ls", "-l", "/etc/fstab"], stdout=subprocess.PIPE)
output, err = p.communicate()
print "*** Running ls -l command ***\n", output

==

import subprocess
p = subprocess.Popen(["ping", "-c", "10", "www.siyahsapka.org"], stdout=subprocess.PIPE)
output, err = p.communicate()
print  output

Upvotes: 0

user25148
user25148

Reputation:

I don't know what th self.session.open stuff is, but here is a simple example of how to use subprocess:

import subprocess

p = subprocess.Popen(
    ['echo', 'run', 'your', 'command'],
    stdin=subprocess.PIPE,
    stdout=subprocess.PIPE,
    stderr=subprocess.PIPE)
out, err = p.communicate('')
print '==== exit code:', p.returncode
print '==== stdout:'
print out,
print '==== stderr:'
print err,

Save that to a file and run it from the command line in a Unix-like system.

http://docs.python.org/2/library/subprocess.html has more details and examples.

Upvotes: 1

Related Questions