0

I'm learning to use the subprocess module. I'm trying to run a .bat script under subprocess. The .bat script writes to a file every time it's called by double-clicking, but won't write to the same file when called from subprocess.

It's similar to this question, but in my case I'd much rather adapt the caller python script then the callee .bat script. The .bat is what I'm about to test.

My code follows. Path and name altered for brevity.

import subprocess
p=subprocess.Popen(r"path\foo.bat", shell=True, stdin = subprocess.PIPE, stdout = subprocess.PIPE)
p.communicate(input='\r\n')

I will need to fill in user input as the .bat runs, so (I think) I need to call Popen directly rather than using convenience functions. But other than that direct call I think the issue is not related, because the file writing should occur before the .bat's first need for user input.

Ommitting the communicate call or each of the arguments to Popen were tried and failed.

How can I change my python script to give the .bat file-writing privileges?

The obvious security implications are not relevant to my use case, the .bat is trusted.

EDIT: when implementing Rptk99's suggestion I've noticed that I can replace the path to the .bat with any string (literally, I've tried "hjklhjilhjkl"), and I'll get the same behaviour: the python prompt becomes interactive again. So I have a more fundamental problem: I cannot see any errors returned from the call.

Community
  • 1
  • 1
Emilio M Bumachar
  • 2,532
  • 3
  • 26
  • 30
  • What happens when it tries to write to the same file? Is there an error message or traceback, and if so what is it? Add that to your question (don't put it in a comment down here). – martineau Jul 05 '16 at 14:07
  • 2
    @Emilio M Bumachar I'm not sure this will help but perhaps the PATH is different and if you open the file not using absolute path you may be writing to another place... – Rptk99 Jul 05 '16 at 14:08
  • @martineau I see nothing from the `.bat` in the python prompt, not even what it usually prints to the DOS shell. Should I be seeing something? Is that an argument missing or wrong that would redirect the output to the prompt? – Emilio M Bumachar Jul 05 '16 at 14:11
  • 2
    Could you show the content of `foo.bat`? – falsetru Jul 05 '16 at 14:12
  • `communicate()` returns a tuple `(stdoutdata, stderrdata)` when the subprocess terminates—so you're not even getting that? i.e. `communicate()` doesn't return? – martineau Jul 05 '16 at 14:17
  • @martineau `communicate()` returns `(None, None)` – Emilio M Bumachar Jul 05 '16 at 14:18
  • So you know the .bat script is unable to write to the file how? – martineau Jul 05 '16 at 14:20
  • @martineau The os date does not update. But see the edit. – Emilio M Bumachar Jul 05 '16 at 14:21
  • When you double click a bat file, the current directory is the directory where the bat file is. When you start it the way you show with subprocess.Popen, the current directory is what is was in the Python script. Maybe you never found the bat file if the path is relative and the current directory is not what you expected, or the bat file wrote to disk but not under the expected folder. Hard to guess without more info on the bat file... – Serge Ballesta Jul 05 '16 at 14:23
  • Sounds like the .bat file _is_ writing to a file somewhere, otherwise there would be some sort of error generated somewhere. – martineau Jul 05 '16 at 14:24
  • With `stdout=subprocess.PIPE`, `communicate` should return `('some string', None)`, not `(None, None)`. – Eryk Sun Jul 05 '16 at 14:28
  • You do not need `shell=True` to run a batch file. Because you aren't setting `stderr=subprocess.PIPE` and you don't call `p.wait()`, you're not seeing the shell's error from not being able to find the batch file. If you don't use `shell=True`, you'll see the error from when `CreateProcess` fails. – Eryk Sun Jul 05 '16 at 14:33
  • @eryksun I can see the real errors now, thank you. They are about sub-scripts called by the .bat, I think I might be able to debug this alone, there's too much context to communicate. If you turn your comment into an answer, I'll accept it. – Emilio M Bumachar Jul 05 '16 at 14:37
  • Thank you all for the help. – Emilio M Bumachar Jul 05 '16 at 14:37

0 Answers0