Python Forum
Creating dictionary from list
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Creating dictionary from list
#1
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()
Reply
#2
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
Reply
#3
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
Reply
#4
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?
Reply
#5
(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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Dictionary in a list bashage 2 568 Dec-27-2023, 04:04 PM
Last Post: deanhystad
  filtering a list of dictionary as per given criteria jss 5 698 Dec-23-2023, 08:47 AM
Last Post: Gribouillis
  Sort a list of dictionaries by the only dictionary key Calab 1 501 Oct-27-2023, 03:03 PM
Last Post: buran
  How to add list to dictionary? Kull_Khan 3 1,018 Apr-04-2023, 08:35 AM
Last Post: ClaytonMorrison
  check if element is in a list in a dictionary value ambrozote 4 1,991 May-11-2022, 06:05 PM
Last Post: deanhystad
  Dictionary from a list failed, help needed leoahum 7 1,978 Apr-28-2022, 06:59 AM
Last Post: buran
  how to assign items from a list to a dictionary CompleteNewb 3 1,594 Mar-19-2022, 01:25 AM
Last Post: deanhystad
  Python, how to manage multiple data in list or dictionary with calculations and FIFO Mikeardy 8 2,619 Dec-31-2021, 07:47 AM
Last Post: Mikeardy
  Class-Aggregation and creating a list/dictionary IoannisDem 1 1,930 Oct-03-2021, 05:16 PM
Last Post: Yoriz
  Python dictionary with values as list to CSV Sritej26 4 3,029 Mar-27-2021, 05:53 PM
Last Post: Sritej26

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020