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.
runBatPy folder ┣ ・ runPythonScript.bat ┗・PythonScript.py Put PythonScript.py in the same location as runPythonScript.bat to run.
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
C:\Python27\python.exe
I think that there are times when the environment variable is not set or you do not want to change it, so pass it directly.
PythonScript.py
Script file you want to run in python
%*
Now you can see all the paths of files and folders dragged and dropped into runPythonScript.bat.
I'm passing it to PythonScript.py.
Other than % *
, you can see what you received with% 1 to% 9
, but this method can only see up to nine.
C:\Python27\python.exe PythonScript.py %* %IN%
This will also pass the% IN% value to 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