LoginSignup
1
1

More than 5 years have passed since last update.

なんかのコマンドを実行して、その結果に特定の文字列が含まれる場合に表示するだけのプログラムをPythonで書いてみた

Posted at

Pythonに閉じない処理をさせながら、結果をPythonで処理したい時のテンプレになるといいかなと思いつつ。
dirコマンドの結果に"xml"という文字列が含まれてたら、該当行を出力という風情。
os.popen()使ってもよかったんだけど、subprocess.Popen()使って書いてみたり。
…例によって残骸付きですw

command.py
#
# coding: utf-8
#
# コマンド実行して、特定の文字列が含まれていたら表示するだけのプログラム。
#

import sys

import os
import subprocess

def main():
    p = subprocess.Popen("dir", shell=True, stdout=subprocess.PIPE,  stderr=subprocess.STDOUT)
    for line in iter(p.stdout.readline, b''):
        if("xml" in line):
            line = line.replace('\n','')
            line = line.replace('\r','')
            print(line)

if (__name__ == "__main__"):
    main()
1
1
4

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
1
1