I replaced the Windows PowerShell cookbook with a python script.

I will need python for my future work, so I decided to make a lot of python scripts.

Environment: python3

Recipe 1 A script that executes an external command in python

exec_shell.py


# coding: utf-8

"""how to use
exec_shell.An xml file containing the py command
Parse the command tag in the xml file that contains the command
A script that executes a parsed command
"""

#Module import
from subprocess import Popen,PIPE
import xml.etree.ElementTree as et
import sys

#Variable definition
cmdfile=sys.argv[1]
tree=et.parse(cmdfile)
cmd=tree.find(".//code").text

#Function definition
def main(cmd):
    p=Popen(cmd,shell=True,stdout=PIPE,stderr=PIPE)
    result=p.communicate()
    return result

#Main processing
if __name__ == '__main__':
    result=main(cmd)
    print('Standard output result: ' + str(result[0]))
    print('Standard error output result: ' + str(result[1]))
    
    

Point 1 Execution of external commands using the subprocess module

Right now, it seems normal to use the subprocess module instead of using "os.commands". It seems that subprocess.call () can only handle the return value of the command, so I will use Popen.

Point 2 The execution result of the external command is stored in the tuple.

In this script, the standard output result is stored in result [0], and the standard error output result is stored in result [1].

Point 3 Command to execute

The command to be executed, including redirection, is put in the cmd variable. This means that if the command changes, you only need to change the cmd variable.

We plan to add a little more error handling.

Recommended Posts

I replaced the Windows PowerShell cookbook with a python script.
Building a Python 3.6 environment with Windows + PowerShell
A memo that I touched the Datastore with python
I made a fortune with Python.
I tried replacing the Windows 10 HDD with a smaller SSD
I liked the tweet with python. ..
I tried changing the python script from 2.7.11 to 3.6.0 on windows10
I made a daemon with Python
Write a batch script with Python3.5 ~
I wanted to solve the ABC164 A ~ D problem with Python
I made a character counter with Python
I drew a heatmap with seaborn [Python]
Search the maze with the python A * algorithm
Run the Python interpreter in a script
I tried a functional language with Python
What I did with a Python array
I made a Hex map with Python
I made a roguelike game with Python
I made a simple blackjack with Python
I made a configuration file with Python
I made a neuron simulator with Python
I replaced the numerical calculation of Python with Rust and compared the speed
I get a Python No module named'encodings' error with the aws command
[Python, ObsPy] I drew a beach ball on the map with Cartopy + ObsPy.
I tried "smoothing" the image with Python + OpenCV
[Python] Get the files in a folder with Python
I made a competitive programming glossary with Python
I made a weather forecast bot-like with Python.
I made a GUI application with Python + PyQt5
I tried "differentiating" the image with Python + OpenCV
I made a Twitter fujoshi blocker with Python ①
I want to make a game with Python
[Python] I made a Youtube Downloader with Tkinter.
I tried "binarizing" the image with Python + OpenCV
Examine Python script bottlenecks with the cProfile module
I want to write to a file with Python
I made a bin picking game with Python
I made a Mattermost bot with Python (+ Flask)
I made a script to record the active window using win32gui of Python
Python starting with Windows 7
I learned Python with a beautiful girl at Paiza # 02
I made a Twitter BOT with GAE (python) (with a reference)
I learned Python with a beautiful girl at Paiza # 01
I made a Christmas tree lighting game with Python
I tried to touch the CSV file with Python
I tried to draw a route map with Python
I tried to solve the soma cube with python
I made a net news notification app with Python
Make a breakpoint on the c layer with python
I made a Python3 environment on Ubuntu with direnv.
I want to inherit to the back with python dataclass
Fill the background with a single color with OpenCV2 + Python
I want to work with a robot in python.
I made a LINE BOT with Python and Heroku
I tried to automatically generate a password with Python3
I tried to solve the problem with Python Vol.1
[Python, ObsPy] I wrote a beach ball with Matplotlib + ObsPy
[Piyopiyokai # 1] Let's play with Lambda: Creating a Python script
I want to run a quantum computer with Python
I moved the automatic summarization API "summpy" with python3.
I tried hitting the API with echonest's python client