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

asked Aug 30, 2015 at 14:43
4
  • 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. Commented Aug 30, 2015 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). Commented Aug 30, 2015 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. Commented Aug 30, 2015 at 15:30
  • @eryksun My fault, that exactly works like a charm. Thanks a lot !! Commented Aug 31, 2015 at 15:23

2 Answers 2

1

You can use this

from subprocess import Popen
p = Popen("batch.bat", cwd=r"C:\Path\to\batchfolder")
stdout, stderr = p.communicate()
answered Aug 30, 2015 at 15:13
Sign up to request clarification or add additional context in comments.

1 Comment

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.
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

answered Aug 30, 2015 at 15:13

3 Comments

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...
I'v updated my answer with a more precise example.
Also, make sure abcdexport.exe is in the PATH, or use dirname (abcdexport.exe) as working directory (for popen args)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.