Open .bat from python [duplicate] - python

This question already has answers here:
Run a .bat file using python code
(9 answers)
Closed 5 years ago.
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

You can use this
from subprocess import Popen
p = Popen("batch.bat", cwd=r"C:\Path\to\batchfolder")
stdout, stderr = p.communicate()

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

Related

How to run shell-commands in Python? [duplicate]

This question already has answers here:
How to run Python's subprocess and leave it in background
(2 answers)
Closed 1 year ago.
This post was edited and submitted for review 1 year ago and failed to reopen the post:
Original close reason(s) were not resolved
I made a remote-control client which can receive commands from the server. The commands that are received will be executed as normal cmd commands in a shell. But how can I execute these commands in the background so that the user wont see any in- or output.
For example when I do this, the user would see anything whats going on:
import os
os.system(command_from_server)
You can using subprocess Popen to start a cmd without waiting for end:
from subprocess import Popen
pid = Popen(["ls", "-l"]).pid
Popen has a lot of configure options for handling stdout and stderr. See the ufficial doc.
To execute a command in background, you have to use the subprocess module.
For example:
import subprocess
subprocess.Popen("command", shell=True)
If you want to execute command with more than one argument eg: ls -a, the code is a bit different:
import subprocess
subprocess.Popen("ls -a", shell=True)
To change the directory you can use the os module:
import os
os.chdir(path)
But, as mentioned from tripleee in the comments bellow, you can also pass the cwd parameter to the subprocess.Popen-Method.

Python subprocess.Popen for multiple python scripts

I am trying to understand the Popen method. I currently have three python files in the same directory: test.py, hello.py and bye.py. test.py is the file containing the subprocess.Popen method while hello and bye are simple hello world and goodbye world files i.e. they only contain a single print.
if I do:
import subprocess
from subprocess import PIPE
tst = subprocess.Popen(["python", "hello.py"], stdout=PIPE, stderr=PIPE)
(out,err) = tst.communicate()
Everything seems to work fine, obtaining in the shell the proper "Hello World" print for the hello.py and doing the same for bye.py the shell prints "GoodBye World" as it should.
The issue starts when I want to run both files,
import subprocess
from subprocess import PIPE
tst = subprocess.Popen(["python", "hello.py", "python", "bye.py"], stdout=PIPE, stderr=PIPE)
(out,err) = tst.communicate()
This will only return the print for the first .py file, and then return
[WinError 2 ] The system cannot find the file specified
This will happen if I also remove the second "python". Why is this happening?
This will happen if I also remove the second "python". Why is this happening?
Running
subprocess.Popen(["python", "hello.py", "python", "bye.py"]
is akin to running
$ python hello.py python bye.py
which doesn't really make a whole lot of sense, since that's interpreted as passing the arguments hello.py python bye.py to python.
So that's part 1, to your question "I am trying to understand the Popen method."
Without knowing what you actually want to do with this proof of concept, you have a few options; call multiple Popen() sequentially, or use a semicolon with shell=True, but make sure to consider the security implications of that:
# This will also break on Windows
>>> import subprocess as sp
>>> sp.check_output("python -V ; python -V", shell=True)
b'Python 3.8.2\nPython 3.8.2\n'

Execute shell command and retrieve stdout in Python [duplicate]

This question already has answers here:
How do I execute a program or call a system command?
(65 answers)
Convert POSIX->WIN path, in Cygwin Python, w/o calling cygpath
(4 answers)
Closed 7 years ago.
In Perl, if I want to execute a shell command such as foo, I'll do this:
#!/usr/bin/perl
$stdout = `foo`
In Python I found this very complex solution:
#!/usr/bin/python
import subprocess
p = subprocess.Popen('foo', shell=True, stdout=subprocess.PIPE, stderr=subprocess.STDOUT)
stdout = p.stdout.readlines()
retval = p.wait()
Is there any better solution ?
Notice that I don't want to use call or os.system. I would like to place stdout on a variable
An easy way is to use sh package.
some examples:
import sh
print(sh.ls("/"))
# same thing as above
from sh import ls
print(ls("/"))
Read more of the subprocess docs. It has a lot of simplifying helper functions:
output = subprocess.check_output('foo', shell=True, stderr=subprocess.STDOUT)
You can try this
import os
print os.popen('ipconfig').read()
#'ipconfig' is an example of command

Python command execution output [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
Running shell command from python and capturing the output
I want to capture the output of a command into a variable, so later that variable can be used again. I need to change this script so it does that:
#!/usr/bin/python
import os
command = raw_input("Enter command: ")
os.system(command)
If I enter "ls" when I run this script, I get this output:
Documents Downloads Music Pictures Public Templates Videos
I want to capture that string (the output of the ls command) into a variable so I can use it again later. How do I do this?
import subprocess
command = raw_input("Enter command: ")
p = subprocess.Popen(command, stdout=subprocess.PIPE)
output, error = p.communicate()
The output of the command can be captured with the subprocess module, specifically, the check_output function..
output = subprocess.check_output("ls")
See also the documentation for subprocess.Popen for the argument list that check_output takes.
This is the way I've done it in the past.
>>> import popen2
__main__:1: DeprecationWarning: The popen2 module is deprecated. Use the subprocess module.
>>> exec_cmd = popen2.popen4("echo shell test")
>>> output = exec_cmd[0].read()
>>> output
'shell test\n'

Run a .bat file using python code

I try to run a .bat file in Windows using Python script.
ask.bat file:
Application.exe work.xml
I write Python code :
import os
os.system("D:\xxx1\xxx2XMLnew\otr.bat ")
Output: when try to run the file its just give a blink of the command prompt, and the work is not performing.
Note: I try with alternate slash also , but it is not working.
And I also want to save output of the file in another file.
Can anyone suggest how can I make the script runnable.
This has already been answered in detail on SO. Check out this thread, It should answer all your questions:
Executing a subprocess fails
I've tried it myself with this code:
batchtest.py
from subprocess import Popen
p = Popen("batch.bat", cwd=r"C:\Path\to\batchfolder")
stdout, stderr = p.communicate()
batch.bat
echo Hello World!
pause
I've got the batchtest.py example from the aforementioned thread.
import subprocess
filepath="D:/path/to/batch/myBatch.bat"
p = subprocess.Popen(filepath, shell=True, stdout = subprocess.PIPE)
stdout, stderr = p.communicate()
print p.returncode # is 0 if success
Replace \ with / in the path
import os
os.system("D:/xxx1/xxx2XMLnew/otr.bat ")
Probably the simplest way to do this is ->
import os
os.chdir("X:\Enter location of .bat file")
os.startfile("ask.bat")
It is better to write .bat file in such way that its running is not dependent on current working directory, i.e. I recommend to put this line at the beginning of .bat file:
cd "%~dp0"
Enclose filepath of .bat file in double quotes, i.e.:
os.system('"D:\\x\\so here can be spaces\\otr.bat" ["<arg0>" ["<arg1>" ...]]')
To save output of some batch command in another file you can use usual redirection syntax, for example:
os.system('"...bat" > outputfilename.txt')
Or directly in your .bat file:
Application.exe work.xml > outputfilename.txt
You are just missing to make it raw. The issue is with "\". Adding r before the path would do the work :)
import os
os.system(r"D:\xxx1\xxx2XMLnew\otr.bat")
So I do in Windows 10 and Python 3.7.1 (tested):
import subprocess
Quellpfad = r"C:\Users\MeMySelfAndI\Desktop"
Quelldatei = r"\a.bat"
Quelle = Quellpfad + Quelldatei
print(Quelle)
subprocess.call(Quelle)
python_test.py
import subprocess
a = subprocess.check_output("batch_1.bat")
print a
This gives output from batch file to be print on the python IDLE/running console. So in batch file you can echo the result in each step to debug the issue. This is also useful in automation when there is an error happening in the batch call, to understand and locate the error easily.(put "echo off" in batch file beginning to avoid printing everything)
batch_1.bat
echo off
echo "Hello World"
md newdir
echo "made new directory"
If you are trying to call another exe file inside the bat-file.
You must use SET Path inside the bat-file that you are calling.
set Path should point into the directory there the exe-file is located:
set PATH=C:\;C:\DOS {Sets C:\;C:\DOS as the current search path.}

Categories