Python, Deleting all files in a folder older than X days
我正在尝试编写一个python脚本来删除一个超过x天的文件夹中的所有文件。这就是我目前为止所拥有的:
1 2 3 4 5 6 7 8 9 10 11 12 13 | import os, time, sys path = r"c:\users\%myusername%\downloads" now = time.time() for f in os.listdir(path): if os.stat(f).st_mtime < now - 7 * 86400: if os.path.isfile(f): os.remove(os.path.join(path, f)) |
当我运行脚本时,我得到:
它给出了文件名。我做错什么了?
最简单的解决方案就是在循环的顶部执行一次:
1 | f = os.path.join(path, f) |
现在,
您还需要给它指定路径,否则它将在CWD中查找。讽刺的是,你在
1 2 | for f in os.listdir(path): if os.stat(os.path.join(path,f)).st_mtime < now - 7 * 86400: |
我认为新的Pathlib Thingy和用于日期的箭头模块使代码更加整洁。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | from pathlib import Path import arrow filesPath = r"C:\scratch emoveThem" 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 |
- pathlib使列出目录内容、访问文件特征(如创建时间)和获取完整路径变得容易。
- 箭头使计算时间更加容易和整洁。
这是显示pathlib提供的完整路径的输出。(无需加入。)
1 2 3 4 5 6 7 8 | C:\scratch emoveThem\four.txt C:\scratch emoveThem\one.txt C:\scratch emoveThem\three.txt C:\scratch emoveThem\two.txt |
一个简单的python脚本,用于删除超过10天的/logs/文件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 | #!/usr/bin/python # run by crontab # removes any files in /logs/ older than 10 days import os, sys, time from subprocess import call def get_file_directory(file): return os.path.dirname(os.path.abspath(file)) now = time.time() cutoff = now - (10 * 86400) files = os.listdir(os.path.join(get_file_directory(__file__),"logs")) file_path = os.path.join(get_file_directory(__file__),"logs/") for xfile in files: if os.path.isfile(str(file_path) + xfile): t = os.stat(str(file_path) + xfile) c = t.st_ctime # delete file if older than 10 days if c < cutoff: os.remove(str(file_path) + xfile) |
使用
有一个脚本只在空间不足时删除文件,这非常适合于生产环境中的日志和备份。
如果不添加新备份,则删除旧文件将最终删除所有备份
https://gist.github.com/pavelniedoba/811a193e8a71286f72460510e1d2d9e9
您需要使用
我发现使用
1 2 3 4 5 6 7 8 9 10 11 | import os, time path = r"c:\users\%myusername%\downloads" now = time.time() for filename in os.listdir(path): # if os.stat(os.path.join(path, filename)).st_mtime < now - 7 * 86400: if os.path.getmtime(os.path.join(path, filename)) < now - 7 * 86400: if os.path.isfile(os.path.join(path, filename)): print(filename) os.remove(os.path.join(path, filename)) |
希望添加我为完成此任务而想到的内容。在登录过程中调用函数。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | def remove_files(): removed=0 path ="desired path" # Check current working directory. dir_to_search = os.getcwd() print"Current working directory %s" % dir_to_search #compare current to desired directory if dir_to_search !="full desired path": # Now change the directory os.chdir( desired path ) # Check current working directory. dir_to_search = os.getcwd() print"Directory changed successfully %s" % dir_to_search for dirpath, dirnames, filenames in os.walk(dir_to_search): for file in filenames: curpath = os.path.join(dirpath, file) file_modified = datetime.datetime.fromtimestamp(os.path.getmtime(curpath)) if datetime.datetime.now() - file_modified > datetime.timedelta(hours=1): os.remove(curpath) removed+=1 print(removed) |
这将删除超过60天的文件。
1 2 3 4 5 6 | import os directory = '/home/coffee/Documents' os.system("find" + directory +" -mtime +60 -print") os.system("find" + directory +" -mtime +60 -delete") |