從已知的shell腳本編寫我的第一個Python3腳本,並且find命令和Python等價物遇到了一個小問題。Python等價於shell查找命令
目錄中有幾天的pcap文件。用戶輸入定義開始日期+時間和結束日期+時間。
這是在shell腳本中使用的命令和輸出。看下面的cut命令會擦除除文件名外的所有內容。開始和結束時間是變量,所以在下面的例子中,我用實際的日期+時間代替了。
find /mnt/pcap/captures/ -type f -newermt "08/08 08:00" ! -newermt "08/08 08:02" | cut -c20-40
full_cap_1354
full_cap_1353
full_cap_1348
full_cap_1350
full_cap_1349
full_cap_1352
full_cap_1351
我確實找到了一種方法讓Python運行os find命令,但似乎無法將開始和結束變量傳遞到命令中。
subprocess.check_output('find /mnt/pcap/captures/ -type f -newermt "$date1" ! -newermt "$date2" | cut -c20-40 > /home/mydir/loading_dock/load.txt', shell = True)
所以,要麼如果有一種方式來傳遞變量,那就是罰款或因爲我正在學習Python的,想有這樣做的一個Python的方式。
你看看'os' python模塊嗎?也許在它的[文件和目錄](https://docs.python.org/2/library/os.html#files-and-directories)部分,你可以找到有用的信息 – araknoid