I want to perform various Linux
commands/operations using python script. I will be using the output, verifying/processing it and continue with some more commands execution in my script, may be remote execution also sometimes.
I have tried with both os
and subprocess
modules. The caveat here is, I am not able to combine both of them i.e. system calls or commands executed from one module does not affect "program/python" environment variables rather only considered by that particular module.
For. ex.
os.chdir(dirname)
os.system(cmd)
# p = subprocess.Popen(cmd)
Now, here changes from os.chdir
are not useful for subprocess call. We have to stick with any one of them. If I use subprocess
, I have to pass/create shell commands for it.
Added: cwd=
is a solution for subprocess.Popen
but every time I would have to pass option cwd
as argument to future commands, if they all should be run from that dir
Is there a better way where we can use both of these modules together?
Or
Is there any other better module available for command executions.
Also I would like to know "Pros-Cons/Caveats" of both these modules.