import subprocess
output= subprocess.Popen(
("c:\\bin\\batch.bat", "an_argument", "another_argument"),
stdout=subprocess.PIPE).stdout
for line in output:
# do your work here
output.close()
Note that it's preferable to start your batch file with "@echo off".
import subprocess
output= subprocess.Popen(
("c:\\bin\\batch.bat", "an_argument", "another_argument"),
stdout=subprocess.PIPE).stdout
for line in output:
# do your work here
output.close()
Note that it's preferable to start your batch file with "@echo off".
Here is a sample python script that runs test.bat and displays the output:
import os
fh = os.popen("test.bat")
output = fh.read()
print "This is the output of test.bat:", output
fh.close()
Source of test.bat:
@echo off
echo "This is test.bat"
Reading output from batch file which runs a Python script - Stack Overflow
subprocess - How to read batch output in python script - Stack Overflow
Run a .bat file in Windows using Python code? - Stack Overflow
python 3.x - Returns of running a batch file - Stack Overflow
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
I'll make this brief and to the point. I need to run my .py from cmd prompt preferably using .bat files. I would like to know how to run it without a .bat file as well. This is for "Automate the Boring Stuff in Python" Chapter 6.
What I have tried:
I am using Pycharm,virtualenvs,python3.6.
-Add shebang to the very top of .py
Ex: #! or #! python -Make the .bat with this general format
Ex: @py.exe C:\Users\User\PycharmProjects\Password Locker\trysys.py %* @pause
-save as trysys.bat
-Type this is cmd Ex: C:\Users\Valler>C:\Users\User\venv\Password Locker\Scripts\python.exe C:\Users\User\PycharmProjects\Password Locker\trysys.bat
-I get this 'C:\Users\User\venv\Password' is not recognized as an internal or external command, operable program or batch file.
-I've also tried simply running trysys.bat
Possible Problems: Perhaps the issue is that Password Locker contains a space in the directory name? Perhaps the issue is I didn't add a path environmental variable correctly or at all?
Any help would be appreciated. Thanks.
Edit: With the thanks of the users below, I figured it out. So, for future finders of this post, this is for you.
running the .py with Base interpreter even though the .py is in a virtual env
In cmd line: "C:\Users\User\folder your py is in\filename.py" Quotes are necessary because there is a space in the directory name No spaces in the directory name? C:\Users\User\folderyourpyisin\filename.py is acceptable
running the .py using the interpreter in the same virtualenv
In cmd: "C:\Users\User\virtualenvfolder\Scripts\python.exe" "C:\Users\User\folder\folder your py is in\filename.py" Basically, use this specific python.exe to run this .py file. To prove that the right interpreter is being user, you do this in the .py
import sys print(sys.executable) This tells you which interpreter is being called
Output: "C:\Users\User\virtualenvfolder\Scripts\python.exe" "C:\Users\User\folder\folder your py is in\filename.py" C:\Users\User\virtualenvfolder\Scripts\python.exe
What about using batch files? There's a little more work involved but it can be worth the time.
running the .py file by using .bat file (This uses base interpreter!)
-
Make a txt file in the same directory as the .py
-
put this in it
@py.exe C:\Users\User\possiblefolder\possiblefolder\yourscript.py %*
@pause
3. Save as filename.bat 4. Edit PATH in system variables to include the directory path of the .py. It may act like it doesn't/does work when it should/shouldn't. Restart should solve this. just a cause of windows stuff See:https://support.microsoft.com/en-us/help/821761/changes-that-you-make-to-environment-variables-do-not-affect-services
5. Make sure Shebang at the top of the .py
\#! python3
6. Windowskey + r brings up run. 7. type in filename and any arguments 8. should run 9. Didn't run? check .bat file again. check directory names. Did you remember to use quotes if you had spaces in the directory name? Did you add the shebang at the top of the .py file? Did you restart after adding the PATH variable. Double check ALL of that.
running the .py file by using .bat file (Using the virtual environment interpreter)
-
Make a txt file in the same directory as the .py
-
put this in it
@C:\Users\User\virtualenvironment\possiblefolder\Scriptsr\python.exe C:\Users\User\possiblefolder\possiblefolder\yourscript.py %*
@pause
3. Save as filename.bat 4. Edit PATH in system variables to include the directory path of the .py. It may act like it doesn't/does work when it should/shouldn't. Restart should solve this. just a cause of windows stuff See:https://support.microsoft.com/en-us/help/821761/changes-that-you-make-to-environment-variables-do-not-affect-services
5. Make sure Shebang at the top of the .py
\#! python3
6. Windowskey + r brings up run. 7. type in filename and any arguments 8. should run
Hope that helps. Thanks for the help from everyone in the thread.
Yes, it's because of the space.
Honestly, I have no idea why the book tells you to write a batch script just to activate a python script. You can just run the python script directly: py path/to/python/script.py.
Did you make sure your Python.exe is added to tour computers system path variable. What happens if you open a cmd prompt and type Python and hit enter?
I currently have a python script that needs to start a batch script. I have the python script inside a folder, with a subdirectory inside of it that contains my batch file. Is there any way that I can possibly have my python script start the batch file?
Why are you calling list2cmdline? This doesn't actually call the subprocess.
Use subprocess.check_output instead:
import os
output = []
for _, _, files in os.walk(directory):
for f in files:
fullpath = os.path.join(directory, os.path.basename(f))
output.append(subprocess.check_output([fullpath]))
print '\n'.join(output)
To write result (output) of a command to a file, you could use stdout parameter:
import os
from glob import glob
from subprocess import check_call
for path in glob(os.path.join(directory, "*.bat")):
name, _ = os.path.splitext(path)
with open(name + ".result", "wb") as outfile:
check_call(path, stdout=outfile)