Delete files that are older than 7 days

后端 未结 3 548
慢半拍i
慢半拍i 2021-01-05 11:04

I have seen some posts to delete all the files (not folders) in a specific folder, but I simply don\'t understand them.

I need to use a UNC path and delete all the f

3条回答
  •  孤街浪徒
    2021-01-05 11:40

    Another version:

    import os
    import time
    import sys
    
    if len(sys.argv) != 2:
        print "usage", sys.argv[0], " "
        sys.exit(1)
    
    workdir = sys.argv[1]
    
    now = time.time()
    old = now - 7 * 24 * 60 * 60
    
    for f in os.listdir(workdir):
        path = os.path.join(workdir, f)
        if os.path.isfile(path):
            stat = os.stat(path)
            if stat.st_ctime < old:
                print "removing: ", path
                # os.remove(path) # uncomment when you will sure :)
    

提交回复
热议问题