import os root = r"D:\_back" for dirpath, dirnames, filenames in os.walk(root): for dirname in dirnames: _dir = os.path.join(dirpath,dirname) lists = os.listdir(_dir) lists = lists[::-1] save = 7 if (len(lists) > save) : for i in range(save - 1, len(lists)-1): f = os.path.join(dirpath,dirname,lists[i]) os.remove(f) print(f)
标签:save,遍历,Python,lists,文件夹,dirpath,dirname,os From: https://www.cnblogs.com/huangcong/p/16946332.html