0

I have a .bat in a folder with an exe named abcexport.exe with the following code inside :

abcexport.exe myswf.swf

Double-clicking the bat as normally on windows exports the swf as expected.

I need to do this from within python, but it complains abcexport is “not recognized as an internal or external command”.

My code :

Attempt 1 -

os.startfile("path\\decompiler.bat")

Attempt 2 -

subprocess.call([path\\decompiler.bat"])

Also tried the same with os.system(), and with subprocess method Popen, and passing the argument shell=True ends up in the same

Nico
  • 43
  • 5
  • 2
    Using `shell=True` is unnecessary for running a .bat with `Popen` (and `call`, etc). The underlying `CreateProcess` call knows to run the shell that's set in the `ComSpec` environment variable. – Eryk Sun Aug 30 '15 at 15:25
  • 2
    Since the batch file relies on the working directory, you need to set it using the `cwd` parameter. That said, the .bat is badly written. It should reference its directory as `%~dp0`, i.e. the [d]rive and [p]ath of the script (arg 0). – Eryk Sun Aug 30 '15 at 15:26
  • 2
    When you run the batch file by double-clicking, it works because Explorer sets the working directory to the directory of the batch file. – Eryk Sun Aug 30 '15 at 15:30
  • @eryksun My fault, that exactly works like a charm. Thanks a lot !! – Nico Aug 31 '15 at 15:23

2 Answers2

1

You can use this

from subprocess import Popen
p = Popen("batch.bat", cwd=r"C:\Path\to\batchfolder")
stdout, stderr = p.communicate()
  • 1
    Use the relative or absolute path to batch.bat as well, unless it's on the search `PATH` or in the working directory of the current process. – Eryk Sun Aug 30 '15 at 16:01
0

A .bat file is not executable. You must use cmd.exe (interpretor) to "run it". Try

import subprocess
executable="path\\decompiler.bat"
p = subprocess.Popen(["C:\Windows\System32\cmd.exe", executable, 'myswf.swf'], shell=True, stdout = subprocess.PIPE)
p.communicate()

in order of the .bat to work

131
  • 3,071
  • 31
  • 32
  • `WindowsError: [Error 2] El sistema no puede encontrar el archivo especificado` -> Can't find the file.. same replacing `\\` with `\\\`, `/`, `//`, with `r"C:\Windows\System32..."`, and with bot `os` and `subprocess` methods... – Nico Aug 30 '15 at 15:35
  • I'v updated my answer with a more precise example. – 131 Aug 30 '15 at 16:28
  • 1
    Also, make sure abcdexport.exe is in the PATH, or use dirname (abcdexport.exe) as working directory (for popen args) – 131 Aug 30 '15 at 16:50