擅长:python、mysql、java
<p>我认为新的pathlib和日期的箭头模块可以使代码更整洁。</p>
<pre><code>from pathlib import Path
import arrow
filesPath = r"C:\scratch\removeThem"
criticalTime = arrow.now().shift(hours=+5).shift(days=-7)
for item in Path(filesPath).glob('*'):
if item.is_file():
print (str(item.absolute()))
itemTime = arrow.get(item.stat().st_mtime)
if itemTime < criticalTime:
#remove it
pass
</code></pre>
<ul>
<li><strong>pathlib</strong>使列出目录内容、访问文件特性(如创建时间)和获取完整路径变得容易。</li>
<li><strong>箭头</strong>使计算时间变得更简单、更整洁。</li>
</ul>
<p>下面的输出显示了pathlib提供的完整路径。(无需加入。)</p>
<pre><code>C:\scratch\removeThem\four.txt
C:\scratch\removeThem\one.txt
C:\scratch\removeThem\three.txt
C:\scratch\removeThem\two.txt
</code></pre>