在Python中,可以使用os
模块来删除超过3个月的名为trash
的文件夹。具体步骤如下:
os
模块:import os
current_time = datetime.datetime.now()
three_months_ago = current_time - relativedelta(months=3)
for root, dirs, files in os.walk(directory):
trash
并且创建日期早于3个月前:if os.path.basename(root) == 'trash' and os.path.getctime(root) < three_months_ago.timestamp():
shutil.rmtree(root)
完整代码示例:
import os
import datetime
from dateutil.relativedelta import relativedelta
import shutil
def delete_old_trash(directory):
current_time = datetime.datetime.now()
three_months_ago = current_time - relativedelta(months=3)
for root, dirs, files in os.walk(directory):
if os.path.basename(root) == 'trash' and os.path.getctime(root) < three_months_ago.timestamp():
shutil.rmtree(root)
# 调用函数删除指定目录下超过3个月的名为'trash'的文件夹
delete_old_trash('/path/to/directory')
这段代码会遍历指定目录下的所有文件和文件夹,找到名为trash
且创建日期早于3个月前的文件夹,然后删除这些文件夹及其内容。
推荐的腾讯云相关产品:对象存储(COS),详情请参考腾讯云对象存储(COS)。
领取专属 10元无门槛券
手把手带您无忧上云