Nico Schlömer
Nico Schlömer

Reputation: 58951

subprocess.check_call with stdin

I would like to use

subprocess.check_call(cmd)

with the stdin argument. Most tutorials I've found so far will recommend using Popen directly (e.g., here), but I really need the exception if cmd errors out.

Any hint on how to get

import subprocess

subprocess.check_call('patch -p1 < test.patch')

to work properly?

Upvotes: 1

Views: 1020

Answers (2)

jfs
jfs

Reputation: 414915

There is no need to run the shell, you could pass a file object as stdin:

with open('test.patch', 'rb', 0) as file:
    subprocess.check_call(['patch', '-p1'], stdin=file)

Upvotes: 4

John Zwinck
John Zwinck

Reputation: 249652

Easy:

subprocess.check_call('patch -p1 < test.patch', shell=True)

Upvotes: 1

Related Questions