0

Command prompt:

C:\Users\Documents\libexe\tfc\bin\Debug>asc-dir
asc-dir.: directory not linked to an ASC directory //Expected output

Test Script:

proc = subprocess.Popen('asc-dir', stdout=subprocess.PIPE, shell = True)
(result, err) = proc.communicate()

This prints "asc-dir.: directory not linked to an ASC directory" to the console but is not saved to result or err.

How can I save the output to result / err?

When I try Popen with shell=False, I get the following error:

Error:
    WindowsError: [Error 2] The system cannot find the file specified
    Press any key to continue . . .
TomSelleck
  • 6,706
  • 22
  • 82
  • 151
  • >The only time you need to specify shell=True on Windows is when the command you wish to execute is built into the shell (e.g. `dir` or `copy`) Do you really need that? – Jahaja Jul 23 '14 at 10:35
  • As I said, when shell=False, I get the error at the bottom of the question. – TomSelleck Jul 23 '14 at 10:46
  • @Jahaja: [`shell=True` and `shell=False` differ in how the executable is found on Windows](http://stackoverflow.com/a/25167402/4279). With `shell=False`, OP might need to provide the extension explicitly e.g., `'asc-dir.cmd'`. – jfs Oct 04 '14 at 15:16

2 Answers2

3

Add stderr=subprocess.PIPE to Popen. Otherwise, the standard error will continue to go to whatever file the subprocess inherits from your script.

chepner
  • 497,756
  • 71
  • 530
  • 681
Jahaja
  • 3,222
  • 1
  • 20
  • 11
0

Remove shell=True unless you actually need it. It sounds like the real problem is that Windows cannot find the asc-dir executable. Where is it installed/located? Is that directory in the PATH environment variable? If not, you could either add it, or specify the full path to the executable when you call Popen e.g.

proc = subprocess.Popen(r'C:\program files\asc\bin\asc-dir', stdout=subprocess.PIPE, stderr=subprocess.PIPE)
Tom Dalton
  • 6,122
  • 24
  • 35
  • That gives me the following error `WindowsError: [Error 193] %1 is not a valid Win32 application` – TomSelleck Jul 23 '14 at 10:52
  • Ahh, so asc-dir isnt a compiled executable, it's a script or something? in which case it's probably fine to run it with shell=True, my Windows-fu is weak ;-) – Tom Dalton Jul 23 '14 at 10:59
  • Yeah it's a batch file, sorry - should have mentioned that in the first place – TomSelleck Jul 23 '14 at 11:03
  • @Tomcelic: have you tried to provide a full name (with extension) e.g., `'asc-dir.exe'` if `shell=False`? Note: I don't think, you need to provide the full path if the command with `shell=True` manages to find `asc-dir` program. – jfs Aug 06 '14 at 15:30
  • @TomDalton: don't use `subprocess.PIPE` unless you read/write using the corresponding pipes. – jfs Aug 06 '14 at 15:32
  • He said he was using communicate(), so the pipes need to be set up to get any data... Ref https://docs.python.org/2/library/subprocess.html#subprocess.Popen.communicate – Tom Dalton Aug 06 '14 at 15:55
  • @TomDalton Shouldn't `\ ` be `/`? And usually we recommend people use `os.path.join` to build path instead of writing in a string. – stanleyxu2005 Oct 04 '14 at 16:03
  • @stanleyxu2005: '\\' is appropriate here, notice `r''`. You shouldn't use `os.path.join()` on a single literal string path. – jfs Oct 05 '14 at 12:49