Best way to rewrite Popen

Jon Ribbens jon+usenet at unequivocal.co.uk
Tue May 19 13:36:18 EDT 2015


On 2015-05-19, Cecil Westerhof <Cecil at decebal.nl> wrote:
> At the moment I am playing with things like:
>     p = subprocess.Popen('ls -l', shell = True, stdout = subprocess.PIPE)
>
> I think that most of the times this are the values I want. So it would
> be nice to overrule the defaults. What is the best way to do this? So
> creating a function that is exactly the same except for the defaults
> for shell and stdout (and maybe stderr).

Yes.

  def shellprocess(cmd, **kwargs):
      return subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE,
          **kwargs)

> It is a little less important as I first thought, because I found the
> following:
>     error, output = subprocess.getstatusoutput('ls -1')
>     files_new = output.splitlines()
> But it is still nice to know.

Why are you doing this anyway, rather than using os.listdir()?
Invoking subprocesses via the shell is very rarely a good idea.



More information about the Python-list mailing list