[Python] Start a batch file from Python and pass variables.

What is a batch file?

** 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)

Why do you need this feature?

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)

Preparation on the Python side

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)

Preparation on the cmd file side

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"

result

At the command prompt, call the Python file you just created with arguments. 2017-02-18_123748.jpg The argument specified on the Python side could be used as an argument of .cmd.

Recommended Posts

[Python] Start a batch file from Python and pass variables.
Execute Python script from batch file
Create a deb file from a python package
Tips: [Python] Randomly restore and extract an array from a fasta file
Run a Python file from html using Django
Read line by line from a file with Python
Batch design and python
Python --Read data from a numeric data file and find the multiple regression line.
Try creating a compressed file using Python and zlib
Make a copy of a Google Drive file from Python
Python-Read data from a numeric data file and calculate covariance
I tried running python etc. from a bat file
Python script to create a JSON file from a CSV file
Python: Class and instance variables
About Python variables and objects
Read and write a file
Start / stop GCE from python
Python class variables and instance variables
Write and read a file
Python variables and object IDs
How to launch AWS Batch from a python client app
I want to start a lot of processes from python
Hash with python and escape from a certain minister's egosa
Python: Create a dictionary from a list of keys and values
Python> Read from a multi-line string instead of a file> io.StringIO ()
Shoot time-lapse from a PC camera using Python and OpenCV
Python --Read data from a numeric data file to find the covariance matrix, eigenvalues, and eigenvectors
Python a + = b and a = a + b are different
Call a Python function from p5.js.
How to batch start a python program created with Jupyter notebook
Get 1000 posts in Python order from all Slack channels and put them together in a txt file
Touch a Python object from Elixir
Python, yield, return, and sometimes yield from
Process Splunk execution results using Python and save to a file
Read and use Python files from Python
Create a binary file in Python
Get data from MySQL on a VPS with Python 3 and SQLAlchemy
Create a simple scheduled batch using Docker's Python Image and parse-crontab
About Python, from and import, as
python / Make a dict from a list.
Batch download images from a specific URL with python Modified version
Python CSV file reading and writing
Read a file in Python with a relative path from the program
Python memo ① Folder and file operations
[Python] return A [or / and] B
Pass a list by reference from Python to C ++ with pybind11
[Python] Inherit a class with class variables
Get OCTA simulation conditions from a file and save with pandas
Write a batch script with Python3.5 ~
Python --Get bitcoin rate BTC / JPY from bitflyer at regular intervals and save it to a file
I want to pass an argument to a python function and execute it from PHP on a web server
2. Make a decision tree from 0 with Python and understand it (2. Python program basics)
Read and format a csv file mixed with comma tabs with Python pandas
Extract lines that match the conditions from a text file with python
When running a Python shell from Electron, pass multiple arguments to run Python.
Migration from Python2 to Python3 (Python2 is rebuilt as a virtual environment and coexists)
Connect to postgreSQL from Python and use stored procedures in a loop.
Make a decision tree from 0 with Python and understand it (4. Data structure)
Create a decision tree from 0 with Python and understand it (5. Information Entropy)
Creating a simple PowerPoint file with Python
From Python to using MeCab (and CaboCha)