** A file that describes a series of commands executed on Windows **. There are two types of batch files with the extensions ".bat" and ".cmd", but the operation is basically the same. If you would like to know more about this difference, please refer to the following article. What is .cmd (extension)
It was a pain to learn batch commands simply. It was easier to process it on the Python side and pass the result as an argument to the batch file. You can use various libraries ... (excuse)
You can pass the process to the specified .cmd file just by describing the path of the .cmd file and the command line argument in the argument of the os.system () function.
You can receive a list of arguments with sys.argv, but since the name of the Python file you started is included at the beginning of the list, exclude this and go around the for loop from index 1.
fuga.py
import os
import sys
cmd_file = "hoge.cmd" # .Path to cmd file
argvs = sys.argv #When having multiple arguments
argc = len(argvs)
command = cmd_file
for i in range(1, argc):
command += " " + argvs[i]
os.system(command)
Write a command that outputs all command line arguments and check if the correct value is obtained.
hoge.cmd
@echo off
echo ".cmd Executed"
for %%f in (%*) do (
echo %%f
)
echo "Completed"
At the command prompt, call the Python file you just created with arguments. The argument specified on the Python side could be used as an argument of .cmd.
Recommended Posts