-1

I'm fairly new to python and I need to get a list of all first level subdirectories sorted inversely by last access date that I can crawl through. The point is I'm writing a cleaning function that receives a variable containing the amount of megabytes to free. It should then walk through all first-level subdirectories and list them according to their last accessed date. Then the function should start deleting them until the needed amount of megabytes is relieved.

My code so far:

import os
def cleanSpace(megs,path="/var/lib/mpd/music/"):
    list = []
    for root, dir, file in os.walk(path):
        this_path = os.path.join(root, dir)
        stat = os.stat(this_path)
        this_atime = stat.st_atime
        this_size = round(stat.st_size/1048576)
        list.append([this_path,this_atime,this_size])
    sort(list, key=lambda x: x[1], reverse=True)
    total_freed = 0
    for folder in list:
        if total_freed < megs:
            #os.unlink(folder[0])
            print(folder[0])
            total_freed += folder[2]
        else:
            print("Total freed space:",total_freed)
            break
chross
  • 511
  • 2
  • 4
  • 13

2 Answers2

2

2 errors I can see immediately:

for root, dir, file in os.walk(path):
    this_path = os.path.join(root, dir)

dir is a list, so you probably mean:

for root, dir, file in os.walk(path):
    for item in dir:
        this_path = os.path.join(root, item)

Note however that os.walk will 'drill down' into all sub-directories - if you want to just get the subdirectories at the top level, you probably mean:

for item in os.listdir(path):
    if os.path.isdir(item):

There is no builtin method called sort - do you mean sorted (or is sort defined elsewhere?)

(Also as per my comment, dir, list and file are builtins you are overriding, so you should choose alternative variable names).

match
  • 10,388
  • 3
  • 23
  • 41
0

Thanks everybody!

One little problem. On my windows machine, os.path.getsize will report wrong numbers.

UPDATE: see below

Here is the final code:

import os
import datetime

def get_size(start_path = '.'):
    total_size = 0
    for dirpath, dirnames, filenames in os.walk(start_path):
        for f in filenames:
            fp = os.path.join(dirpath, f)
            total_size += os.path.getsize(fp)
    return total_size

def cleanSpace(megs,path="c:/Users/i/Downloads/"):
    list = []
    for item in os.listdir(path):
        fullitem=os.path.join(path, item)
        if os.path.isdir(fullitem):
            this_path = fullitem
            stat = os.stat(fullitem)
            this_atime = stat.st_atime
            #print("Last access: ",this_atime)
            #print("Last access: ",datetime.datetime.fromtimestamp(this_atime))
            this_size = get_size(fullitem)
            #size in MB
            this_size = round(this_size/1048576)
            list.append([fullitem,this_atime,this_size])
    list = sorted(list, key=lambda x: x[1], reverse=False)
    total_freed = 0
    for folder in list:
        if total_freed < megs:
            #os.unlink(folder[0])
            print(folder[0] + " - size: " + str(folder[2]))
            total_freed = total_freed + folder[2]
        else:
            print("Total freed space (MB):",total_freed)
            break
    if total_freed < megs:
        print("Not enough space freed")

cleanSpace(1100,)

For folder sizes I'm now using the function get_size() from Calculating a directory's size using Python?

chross
  • 511
  • 2
  • 4
  • 13