pythonでlinux shellスクリプトを実行する

794 ワード

subprocess.Popen(command, shell=True)
commandが実行可能ファイルでない場合、shell=Trueは節約できません.
最も簡単な方法はclass subprocessを使うことです.Popen(command,shell=True).
Popen類にはPopenがある.stdin,Popen.stdout,Popen.stderrの3つの有用な属性は,サブプロセスとの通信を実現できる.
例:
handle = subprocess.Popen('ls -l', stdout=subprocess.PIPE, shell=True)
print handle.stdout.read()
print handle.communicate()[0]
python特殊文字を削除
lstripとrstripはそれぞれ先頭と末尾の文字を除去します
例:
theString = 'saaaay yes no yaaaass'
print theString.strip('say')
print theString.strip('say ') #say     
print theString.lstrip('say')
print theString.rstrip('say') 

結果:
yes no
es no
yes no yaaaass
saaaay yes no