Load and execute command from yml in python

I wrote a program to read the command from the yml file, so I will post it. yml file

cmd1:
        command: ls -l
        type: ls

cmd2:
        command: ls -a
        type: ls

python code

import subprocess
import yaml

with open('sample.yml') as file:
    obj = yaml.safe_load(file)

cmd1 = obj['cmd1']['command']
types = obj['cmd1']['type']

print('The execution mode is', types,'is.')
print(cmd1)
subprocess.run(cmd1, shell=True)

print('\n')

cmd2 = obj['cmd2']['command']
types = obj['cmd2']['type']

print('The execution mode is', types,'is.')
print(cmd2)
subprocess.run(cmd2, shell=True)

Execution result

The run mode is ls.
ls -l
total 12
-rwxrwxrwx 1 guest guest 411 Nov  2 09:40 command.py
-rwxrwxrwx 1 guest guest 380 Nov  2 09:06 memo.md
-rwxrwxrwx 1 guest guest  92 Nov  2 09:27 sample.yml


The run mode is ls.
ls -a
.  ..  command.py  memo.md  sample.yml

Recommended Posts

Load and execute command from yml in python
Execute command from Python
Execute command from python
Execute external command in python
Get options in Python from both JSON files and command line arguments
Load images from URLs using Pillow in Python 3
How to execute a command using subprocess in Python
[Python] Execute unix command (subprocess)
OCR from PDF in Python
Stack and Queue in Python
External command execution in Python
Unittest and CI in Python
Predict gender from name using Gender API and Pykakasi in Python
How to execute external shell scripts and commands in python
Execute Python script from batch file
MIDI packages in Python midi and pretty_midi
Difference between list () and [] in Python
Hit a command in Python (Windows)
View photos in Python and html
Sorting algorithm and implementation in Python
Get data from Quandl in Python
Python, yield, return, and sometimes yield from
Run shell command / python in R
Manipulate files and folders in Python
About dtypes in Python and Cython
Dynamically load json type in python
Read and use Python files from Python
In the python command python points to python3.8
Assignments and changes in Python objects
About Python, from and import, as
Check and move directories in Python
Extract text from images in Python
Ciphertext in Python: IND-CCA2 and RSA-OAEP
Hashing data in R and Python
Execute Python code from C # GUI
Function synthesis and application in Python
Export and output files in Python
Reverse Hiragana and Katakana in Python2.7
Reading and writing text in Python
[GUI in Python] PyQt5-Menu and Toolbar-
Create and read messagepacks in Python
Extract strings from files in Python
How to get a string from a command line argument in python
How to automatically execute python script (with GUI) and sudo permission command after logging in with Ubuntu 18.04
Overlapping regular expressions in Python and Java
Load the remote Python SDK in IntelliJ
Differences in authenticity between Python and JavaScript
Notes using cChardet and python3-chardet in Python 3.3.1.
Modules and packages in Python are "namespaces"
Avoid nested loops in PHP and Python
Get exchange rates from open exchange rates in Python
From Python to using MeCab (and CaboCha)
Differences between Ruby and Python in scope
AM modulation and demodulation in Python Part 2
difference between statements (statements) and expressions (expressions) in Python
Eigenvalues and eigenvectors: Linear algebra in Python <7>
[LLDB] Create your own command in Python
Implementation module "deque" in queue and Python
Revived from "no internet access" in Python
Prevent double boot from cron in Python
Line graphs and scale lines in python