How do I run this command with subprocess?
I tried:
proc = subprocess.Popen(
    '''ECHO bosco|"C:\Program Files\GNU\GnuPG\gpg.exe" --batch --passphrase-fd 0 --output "c:\docume~1\usi\locals~1\temp\tmptlbxka.txt" --decrypt "test.txt.gpg"''',
    stdin=subprocess.PIPE,
    stdout=subprocess.PIPE,
   stderr=subprocess.STDOUT,
)
stdout_value, stderr_value = proc.communicate()
but got:
Traceback (most recent call last):
...
  File "C:\Python24\lib\subprocess.py", line 542, in __init__
    errread, errwrite)
  File "C:\Python24\lib\subprocess.py", line 706, in _execute_child
    startupinfo)
WindowsError: [Errno 2] The system cannot find the file specified
Things I've noticed:
First and foremost, you don't actually need a pipe; you are just sending input. You can use subprocess.communicate for that.
Secondly, don't specify the command as a string; that's messy as soon as filenames with spaces are involved.
Thirdly, if you really wanted to execute a piped command, just call the shell. On Windows, I believe it's cmd /c program name arguments | further stuff.
Finally, single back slashes can be dangerous: "\p" is '\\p', but '\n' is a new line. Use os.path.join() or os.sep or, if specified outside python, just a forward slash.
proc = subprocess.Popen(
    ['C:/Program Files/GNU/GnuPG/gpg.exe',
    '--batch', '--passphrase-fd', '0',
    '--output ', 'c:/docume~1/usi/locals~1/temp/tmptlbxka.txt',
    '--decrypt', 'test.txt.gpg',],
    stdin=subprocess.PIPE,
    stdout=subprocess.PIPE,
   stderr=subprocess.STDOUT,
)
stdout_value, stderr_value = proc.communicate('bosco')
                        You were right, the ECHO is the problem. Without the shell=True option the ECHO command cannot be found.
This fails with the error you saw:
subprocess.call(["ECHO", "Ni"])
This passes: prints Ni and a 0
subprocess.call(["ECHO", "Ni"], shell=True)
                        If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With