Search code examples
pythonsubprocesspopen

How to use '>>' in popen subprocess


I'm trying to run the command from a python file (2.7):

p=subprocess.Popen("sha256sum file1.zip >> file2.sha")

But i got an error that file '>>' does not exist. I tried:

p=subprocess.Popen("sha256sum file1.zip >> file2.sha".split())

But still the >> is a problem.

Of course that if I run the command in the prompt line it run Ok and put the output into the file file2.sha.

I know I can add stdout to the Popen but I was wonder if there is a way to run it as simple as runing from the command line.

Thanks.


Solution

  • You can pass values for the stdin and stdout of the child process to Popen like so:

    subprocess.Popen("sha256sum file1.zip", stdout = file("file2.sha", "a"))
    

    Note the file needs to be opened in append mode to achieve the same behaviour as >>.