簡體   English   中英

如何使用Python在內部具有特定目錄結構的tar文件中實現

[英]How to achieve the tar file with a specific directory structure inside it using Python

我有一個包含許多日志文件的目錄結構。

例:

root/feature1/pre/a.log<br>
root/feature1/pre/b.log<br>
root/feature1/post/c.log<br>
root/feature1/post/d.og<br>
root/feature2/pre/e.log<br>
root/feature2/pre/f.log<br>
root/feature2/post/g.log<br>
root/feature2/post/h.log

我想歸檔一些日志文件,但條件是日志文件必須超過2個月。 我可以在給定條件下歸檔日志文件,但無法在tar文件中維護目錄。

我需要 :
根/ archive.tar.gz

其中archive.tar.gz包含

feature1/pre/a.log
feature1/post/d.log
feature2/pre/e.log
feature2/post/g.log
feature2/post/h.log

在此,a.log,d.log,e.log,g.log,h.log文件是兩個月以上的文件。

這比Python更像是Linux問題。 我建議將find命令與文件名和文件時間選項一起使用。 將這些名稱傳遞到您正在使用的tar命令中,例如

tar -czf archive `find /root -name feature*.log -ctime +60`

在命令行上進行測試; 當它可以根據需要工作時,請使用Python的os包從您的Python腳本執行它。

那會讓你動起來嗎?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM