我在一个有用的Bash脚本中有这一行,我没有设法将其转换为Python,其中'a'是用户输入的存档天数值:
find ~/podcasts/current -mindepth 2 -mtime '+`a`+' -exec mv {} ~/podcasts/old \;
我熟悉最常用的跨平台元素的os.name和getpass.getuser.我也有这个函数来生成相当于〜/ podcasts/current的所有文件的全名列表:
def AllFiles(filepath, depth=1, flist=[]): fpath=os.walk(filepath) fpath=[item for item in fpath] while depth < len(fpath): for item in fpath[depth][-1]: flist.append(fpath[depth][0]+os.sep+item) depth+=1 return flist
首先,必须有更好的方法,任何建议欢迎.无论哪种方式,例如,"AllFiles('/ users/me/music/itunes/itunes music/podcasts')"在Windows上提供相关列表.据推测,我应该能够查看此列表并调用os.stat(list_member).st_mtime并将所有超过特定数字的内容移动到存档中; 我有点卡在那一点上.
当然,任何具有bash命令简洁性的东西也会很有启发性.
import os import shutil from os import path from os.path import join, getmtime from time import time archive = "bak" current = "cur" def archive_old_versions(days = 3): for root, dirs, files in os.walk(current): for name in files: fullname = join(root, name) if (getmtime(fullname) < time() - days * 60 * 60 * 24): shutil.move(fullname, join(archive, name))