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


Messages In This Thread
Creating dictionary from list - by kerzol81 - Oct-17-2017, 07:28 PM
RE: Creating dictionary from list - by nilamo - Oct-17-2017, 07:52 PM
RE: Creating dictionary from list - by kerzol81 - Oct-18-2017, 07:39 AM
RE: Creating dictionary from list - by kerzol81 - Oct-23-2017, 08:55 AM
RE: Creating dictionary from list - by snippsat - Oct-23-2017, 11:40 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Sort a list of dictionaries by the only dictionary key Calab 2 648 Apr-29-2024, 04:38 PM
Last Post: Calab
  Dictionary in a list bashage 2 607 Dec-27-2023, 04:04 PM
Last Post: deanhystad
  filtering a list of dictionary as per given criteria jss 5 756 Dec-23-2023, 08:47 AM
Last Post: Gribouillis
  How to add list to dictionary? Kull_Khan 3 1,054 Apr-04-2023, 08:35 AM
Last Post: ClaytonMorrison
  check if element is in a list in a dictionary value ambrozote 4 2,043 May-11-2022, 06:05 PM
Last Post: deanhystad
  Dictionary from a list failed, help needed leoahum 7 2,053 Apr-28-2022, 06:59 AM
Last Post: buran
  how to assign items from a list to a dictionary CompleteNewb 3 1,624 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,696 Dec-31-2021, 07:47 AM
Last Post: Mikeardy
  Class-Aggregation and creating a list/dictionary IoannisDem 1 1,952 Oct-03-2021, 05:16 PM
Last Post: Yoriz
  Python dictionary with values as list to CSV Sritej26 4 3,078 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