I will need python for my future work, so I decided to make a lot of python scripts.
Environment: python3
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]))
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.
In this script, the standard output result is stored in result [0], and the standard error output result is stored in result [1].
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