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
2 Answers 2
You can use this
from subprocess import Popen
p = Popen("batch.bat", cwd=r"C:\Path\to\batchfolder")
stdout, stderr = p.communicate()
1 Comment
PATH or in the working directory of the current process.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
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...
shell=Trueis unnecessary for running a .bat withPopen(andcall, etc). The underlyingCreateProcesscall knows to run the shell that's set in theComSpecenvironment variable.cwdparameter. 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).