BAKE ZQ
BAKE ZQ

Reputation: 807

How to Use Python Subprocess to Write a String to a File

I am trying to figure out what happens in subprocess. So, I write this code:

import subprocess

p1 = subprocess.Popen('grep a', stdin=subprocess.PIPE, stdout=subprocess.PIPE,shell=True, universal_newlines=True)
p2 = subprocess.Popen('grep a', stdin=p1.stdout, stdout=open('test', 'w'),shell=True, universal_newlines=True)

p1.stdin.write("asdads"*700)

After I write the string to p1.stdin, I expected the string to be written to the file test. But there is nothing in the file.

When I tried another way:

out, err = p1.communicate("adsad"*700)

The string is in out.

Upvotes: 1

Views: 792

Answers (1)

knh190
knh190

Reputation: 2882

Your code wasn't working because your stdin stream is not closed. To prove what I'm saying:

p1 = subprocess.Popen('grep l', stdin=subprocess.PIPE, stdout=subprocess.PIPE, shell=True, universal_newlines=True)
out, err = p1.communicate('hello')

>>> out
'hello\n'

Now test with communicate, which automatically closes stream for you.

p1 = subprocess.Popen('grep l', stdin=subprocess.PIPE, stdout=subprocess.PIPE, shell=True, universal_newlines=True)
p2 = subprocess.Popen('grep h', stdin=p1.stdout, stdout=open('test', 'w'),shell=True, universal_newlines=True)

# test is already created and empty
# but call communicate again can write to file
>>> p1.communicate('hello')
('', None)

$cat test
hello

And another way:

# use stdin.write
>>> p1.stdin.write('hello') # empty file
>>> p1.stdin.close() # flushed

References:

  1. subprocess popen.communicate() vs. stdin.write() and stdout.read()
  2. https://docs.python.org/3/library/subprocess.html#subprocess.Popen.communicate

Upvotes: 2

Related Questions