Execute Python script from batch file

Overview

Execute a Python script from a batch file. The main processing is done by Python script. The batch takes a file or folder and passes it to a Python script.

Preparation

runBatPy folder ┣ ・ runPythonScript.bat ┗・PythonScript.py Put PythonScript.py in the same location as runPythonScript.bat to run.

Sample code: runPythonScript.bat

cd /d %~dp0
C:\Python27\python.exe PythonScript.py %*
cd
pause

Take files etc. and pass those paths to PythonScript. If you don't throw anything, you won't get an error.

cd /d %~dp0 Make the current directory the directory of the executed batch file.

C:\Python27\python.exe PythonScript.py %* From left to right

I want to pass multiple variables to PythonScript

C:\Python27\python.exe PythonScript.py %* %IN% This will also pass the% IN% value to PythonScript.py.

サンプルコード:PythonScript.py

import sys
if __name__ == "__main__":
 param = sys.argv
 for p in param:
  print p

if __name__ == "__main__": Process the inside of the if statement only when it is executed as a script file.

sys.argv argv [0] is the name of the script After that, the path passed by % * is included. Quoted from Python 27 reference

A list of command line arguments passed to the Python script. argv [0] is the name of the script, but whether it is a full path name or not depends on the OS. If you start Python with -c as a command line argument, argv [0] becomes the string'-c'. If you start Python without a script name, argv [0] will be an empty string. To loop over the list of files specified by standard input or command line arguments, see the fileinput module.

Recommended Posts

Execute Python script from batch file
Script python file
Automatically execute python file
Execute command from Python
Execute command from python
Run illustrator script from python
Python script to create a JSON file from a CSV file
[Python] Start a batch file from Python and pass variables.
Execute Python code from C # GUI
Execute Python Script during CodeSys # RunTime
Write a batch script with Python3.5 ~
Script to generate directory from json file
Execute a script from Jupyter to process
Execute Python script with cron of TS-220
Create a deb file from a python package
Use Django from a local Python script
[Note] Execute Python code from Excel (xlwings)
python script skeleton
Python script profiling
Import python script
sql from python
Python file processing
Python handy batch
MeCab from Python
Run a Python script from a C # GUI application
Import Excel file from Python (register to DB)
From file to graph drawing in Python. Elementary elementary
Create a New Todoist Task from Python Script
Periodically execute Python Script on AWS Data Pipeline
Create wav file from GLSL shader using python3
Load and execute command from yml in python
"Python Kit" that calls a Python script from Swift
[Node-RED] Execute Python on Anaconda virtual environment from Node-RED [Anaconda] [Python]
Call a Python script from Embedded Python in C ++ / C ++
Run a Python file from html using Django
Wav file generation from numeric text with python
Read line by line from a file with Python
Run a python script from excel (using xlwings)
A python script that draws a band diagram from the VASP output file EIGENVAL
Use thingsspeak from python
File operations in Python
Touch MySQL from Python 3
[Python] File / directory operations
Periodically execute python script with cron in venv environment
Template of python script to read the contents of the file
Operate Filemaker from Python
[Note] File reading ~ Python ~
File processing in Python
Use fluentd from python
Access bitcoind from python
Changes from Python 3.0 to Python 3.5
Batch design and python
Changes from Python 2 to Python 3.0
Make a copy of a Google Drive file from Python
Python from or import
Use MySQL from Python
I tried running python etc. from a bat file
File name batch change
Run python from excel
Install python from source
A memorandum to run a python script in a bat file