Wednesday, 15 August 2012

Python function that calls bash commands -



Python function that calls bash commands -

i trying create python function take bash command (cmd) argument, , execute command.

but having issues...

this program:

import subprocess def main(): runcommand("ls") runcommand("ls -l") runcommand("cd /") runcommand("ls -l") def runcommand(cmd): subprocess.popen(cmd)

it works commands "ls" or "who" when gets longer such "ls -l" or "cd /" gives me error.

traceback (most recent phone call last): file "<string>", line 1, in ? file "test.py", line 8, in main runcommand("ls -l") file "test.py", line 14, in runcommand subprocess.popen(cmd) file "/usr/lib64/python2.4/subprocess.py", line 550, in __init__ errread, errwrite) file "/usr/lib64/python2.4/subprocess.py", line 996, in _execute_child raise child_exception oserror: [errno 2] no such file or directory

you need set command , alternative in list :

subprocess.popen(['ls','-l'])

python bash

No comments:

Post a Comment