Python Forum

Full Version: Creating dictionary from list
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi,

I'm struggling with a problem. I'd like to create a dictionary which looks like this:

my_dictionary = {'folder1': 'Same', 'folder2': 'Same', 'folder3': 'Different'}
folder1, folder2, folder3 etc are the subfolders of the current workdir.

Different: when folder size is different after 1 minutes
Same: when folder size is the same after 1 minutes

I'm here so far, which is nothing but scratching the surface.
import os

rootdir = '.'

def examine():
    return dict(enumerate(next(os.walk(rootdir))[1]))
I have a function which returns True or False if a dictionary size has changed, but now I have to apply this to the subdirectories. And I thought a dictionary would be a nice way to do it, but I realised in the meantime that I only know what a dictionary is, but I cannot apply this knowledge to a real working solution... If you could help me out, please do it.

Here is another attempt:

import os
import time

rootdir = '.'

def examine():


    while True:

        try:
            folders = next(os.walk(rootdir))[1]
        except StopIteration:
            print('[-] No such folder')
            break

        for f in folders:
            now = os.path.getsize(f)
            time.sleep(2)
            after = os.path.getsize(f)

            if now == after:
                print(f + ': Same')
            else:
                print(f + ': Different')

examine()
Instead of returning, just add each item to a dict:
def examine():
    sizes = {}
    folders = next(os.walk(rootdir))[1]
    for f in folders:
        now = os.path.getsize(f)
        time.sleep(60)
        after = os.path.getsize(f)
        sizes[f] = 'Same' if now == after else 'Different'
    return sizes
Thanks!

Do you know a better way of getting a folder size, since this way I got buffer overflows when I examine with Process Mon:
These folders are huge in size. 

def get_dirsize(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
Hi Folks,

I have a code like this:

def get_latest_file(folder='.'):
    latest_file = max(os.listdir(folder), key=lambda f: os.path.getctime("{}/{}".format(folder, f)))
    creation_time = time.strftime('%Y-%m-%d %H:%M', time.gmtime(os.path.getctime("{}/{}".format(folder, latest_file))))
    return str(latest_file) + ', creation time: ' + str(creation_time)
I don't like it, but it works, it must be a more elegant way of doing what I want with this function. Could you please suggest a better way?
(Oct-23-2017, 08:55 AM)kerzol81 Wrote: [ -> ]Could you please suggest a better way?
Something like this.
import time
from glob import glob
from os.path import getctime

def latest(path='*'):
    file_list = glob(path) 
    latest_file = max(file_list, key=getctime)
    created = time.ctime(getctime(latest_file))
    return f'{latest_file} creation time: {created}'

print(latest())
Output:
test.py creation time: Mon Oct 23 13:39:13 2017