使用python删除未访问的文件 [英] deleting unaccessed files using python

查看:52
本文介绍了使用python删除未访问的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 django应用解析用户上传的一些文件.用户上传的文件可能会长时间保留在服务器中,而不会被应用解析.如果许多用户上传大量文件,则大小可能会增加.

My django app parses some files uploaded by the user.It is possible that the file uploaded by the user may remain in the server for a long time ,without it being parsed by the app.This can increase in size if a lot of users upload a lot of files.

我需要删除应用程序最近未解析的文件-例如最近24小时未访问.我试图这样做

I need to delete those files not recently parsed by the app -say not accessed for last 24 hours.I tried like this

import os
import time

dirname = MEDIA_ROOT+my_folder
filenames = os.listdir(dirname)
filenames = [os.path.join(dirname,filename) for filename in filenames]
for filename in filenames:
    last_access = os.stat(filename).st_atime #secs since epoch
    rtime = time.asctime(time.localtime(last_access))
    print filename+'----'+rtime

这显示了每个文件的上次访问时间..但是我不确定如何测试文件访问时间是否在最近24小时之内..有人可以帮助我吗?

This shows the last accessed times for each file..But I am not sure how I can test if the file access time was within the last 24 hours..Can somebody help me out?

推荐答案

签出 time.time().它将允许您以utc时间访问当前时间戳.然后,您可以从文件时间戳中减去当前戳记,看看它是否大于24 * 60 * 60.

Check out time.time(). It will allow you to access the current timestamp, in utc time. You can then subtract the current stamp from the file timestamp and see if it's greater than 24*60*60.

http://docs.python.org/library/time.html#时间.时间

此外,请记住,在很多时候,Linux文件系统都是使用noatime挂载的,这意味着可能不会填充st_atime变量.为了安全起见,您可能应该使用st_mtime,除非您100%确定文件系统将始终以记录的时间挂载.

Also, keep in mind that a lot of the time, a Linux filesystem is mounted with noatime, meaning the st_atime variable might not be populated. You should probably use st_mtime, just to be safe, unless you're 100% sure the filesystem will always be mounted with atimes recorded.

这是一个可行的示例,但我尚未调试.

Here's what should be a working example, I haven't debugged though.

import os
import time

dirname = MEDIA_ROOT+my_folder
filenames = os.listdir(dirname)
filenames = [os.path.join(dirname,filename) for filename in filenames]
for filename in filenames:
    last_access = os.stat(filename).st_mtime #secs since epoch
    timediff = time.gmtime() - last_access
    print filename+'----'+timediff
    if timediff > 24*60*60:
        print 'older than a day'
        # do your thing

这篇关于使用python删除未访问的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆