0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

pythonでymlからcommandをloadして実行する

Posted at

yml fileからcommandを読み取るプログラムを書いたので掲載しておきます

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('実行モードは', types,'です。')
print(cmd1)
subprocess.run(cmd1, shell=True)

print('\n')

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

print('実行モードは', types,'です。')
print(cmd2)
subprocess.run(cmd2, shell=True)

実行結果

実行モードは 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


実行モードは ls です。
ls -a
.  ..  command.py  memo.md  sample.yml
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?