2013-05-14 19 views
0

我從一個Python腳本內運行的grep像這樣:從os.system使用grep的Python添加「0」行到變量?

last_run_start = os.system("cat %(file)s | grep '[0-24]:[0-59]' | tail -n1" % locals())

哪翻出file最後的時間戳。當我通過Python命令行執行此操作時,或通過常規終端使用該grep命令時,我會得到預期的結果 - 包含時間戳的最後一行。

然而,從這個腳本last_run_start運行時,返回此:

18:23:45 

0 

請告訴我造成這個「0」出現,更不用說在一個新行?更重要的是,我如何從last_run_start中刪除它?

回答

5

os.system返回你運行的命令,在這種情況下似乎是0

命令的輸出直接進入stdout,而不是存儲在last_run_start的退出代碼,如果你想您應該使用subprocess模塊中的Popencheck_output

我想0最終被打印,因爲你打印last_run_start somwhere。