Python, Deleting all files in a folder older than X days

前端 未结 10 632
无人及你
无人及你 2020-12-08 00:39

I\'m trying to write a python script to delete all files in a folder older than X days. This is what I have so far:

import os, time, sys

path = r\"c:\\user         


        
10条回答
  •  半阙折子戏
    2020-12-08 00:51

    You need to give it the path also or it will look in cwd.. which ironically enough you did on the os.remove but no where else...

    for f in os.listdir(path):
        if os.stat(os.path.join(path,f)).st_mtime < now - 7 * 86400:
    

提交回复
热议问题