1

I created a python script for cleaning old files on a windows disk. Now I want to improve that script and check if I went over a predefined disk usage and then delete files ... how I can do it? Here's my code:

import os, time

path = r"f:\backup"
now = time.time()
for f in os.listdir(path):
    if os.stat(f).st_mtime < now - 180 * 86400:
        if os.path.isfile(f):
            os.remove(os.path.join(path, f))

I need it because I've configured a backup of my computer on external disk and sometimes it can't do the backup because disk is full.

Simeon Visser
  • 118,920
  • 18
  • 185
  • 180
Torgia
  • 318
  • 3
  • 13

1 Answers1

1

I would recommend checking out this answer:

Cross-platform space remaining on volume using python

The nt implementation of the os module does not include the statvfs attribute, so you'd need to recreate that functionality using c_types.

Community
  • 1
  • 1
selllikesybok
  • 1,250
  • 11
  • 17