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.

2
  • What behavior are you looking for when disk space is exceeded? What will you remove in that case? Commented Jul 16, 2012 at 14:57
  • BTW, it would likely be a little faster to reverse the order of your if tests. Commented Jul 16, 2012 at 16:17

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.