0

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.

  • @jozefg I have visited that question earlier. It is more about how I can execute one single command, and not for whole script. My question is for multiple commands if I want to combine functions from both `os` and `subprocess`, how can I do that? –  Sep 27 '13 at 12:19
  • 1
    If you are using internal shell commands in a Python script, you are doing it wrong (you should've sticked with a `sh` script). If you want to run other executables, `subprocess` is the correct module. All your questions could be answered by anyone (including you) that properly read the `subprocess` documentation. – KurzedMetal Sep 27 '13 at 12:20
  • Have a look at `envoy`: https://github.com/kennethreitz/envoy `subprocess` can be a bit counter-intuitive at first, `envoy` simplifies things for the use-case of shell scripting, etc. – Joe Kington Sep 27 '13 at 12:43

1 Answers1

2

os.system always runs /bin/sh, which parses the command string. This can be a security risk if you have whitespace, $ etc. in the command arguments, or the user has a shell config file. To avoid all such risks, use subprocess with a list or tuple of strings as the command (shell=False) instead.

To emulate os.chdir in the command, use the cwd= argument in subprocess.

pts
  • 80,836
  • 20
  • 110
  • 183
  • `os.system` and `os.chdir` were more of examples. Lets say if I want to run command with different user. I have `os.setuid()` and in subprocess I would have to do `su user cmd`. –  Sep 27 '13 at 12:30
  • Also with subprocess every time I would have to pass `cwd=` argument to future commands, if they all should be run from that dir. –  Sep 27 '13 at 12:32