1

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?

Community
  • 1
  • 1
Nico Schlömer
  • 53,797
  • 27
  • 201
  • 249

2 Answers2

4

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)
jfs
  • 399,953
  • 195
  • 994
  • 1,670
2

Easy:

subprocess.check_call('patch -p1 < test.patch', shell=True)
John Zwinck
  • 239,568
  • 38
  • 324
  • 436