min117の日記

初期desireもち。趣味Mac,メインFedora,仕事xp。

Python 3 からシェル(bash)のコマンドを実行する

 

Python3 のソース内でbashコマンド(ls -shal)を実行する。

f:id:min117:20170528103338p:plain

実行結果

f:id:min117:20170528103957p:plain

Python3 の注意点

import commands は動作しない

 Python2までしか使えない。Python3 では廃止された。代わりに import subprocess を使う。

・print文の引数にはカッコをつける

 print ("必ずカッコをつける")

 print ("文字列にはダブルクオートをつける")

 print ('シングルクオートでもよい')