[Tutor] storing and saving file tree structure

mhysnm1964 at gmail.com mhysnm1964 at gmail.com
Mon Jan 25 23:09:19 EST 2021


Cameron

Thanks for asking. It all depends on the editor. Notepad++ does both. I just
find it easier to identify the block ends this way. Very non-conforming I
know.

I also found a recipe on how to achieve what I want by using OS.Walk. 

import os, functools, ftplib 

def get_directory_structure(rootdir):
    """
    Creates a nested dictionary that represents the folder structure of
rootdir
    """
    dir = {}
    rootdir = rootdir.rstrip(os.sep)
    start = rootdir.rfind(os.sep) + 1
    for path, dirs, files in os.walk(rootdir):
        if len(dirs) > 0:
            folders = path[start:].split(os.sep)
            subdir = dict.fromkeys(dirs)
            parent = functools.reduce(dict.get, folders[:-1], dir)
            parent[folders[-1]] = subdir
    return dir


#load local books into title dict.
titles = get_directory_structure(r'e:\authors')

If I remove the if test in the function. Then I can create a directory
structure with all the files.  This is a good example of using os.walk.


Sean 

-----Original Message-----
From: Cameron Simpson <cs at cskk.id.au> 
Sent: Tuesday, 26 January 2021 12:46 PM
To: mhysnm1964 at gmail.com
Cc: 'Alan Gauld' <alan.gauld at yahoo.co.uk>; tutor at python.org
Subject: Re: [Tutor] storing and saving file tree structure

On 25Jan2021 20:20, Sean Murphy <mhysnm1964 at gmail.com> wrote:
>As indents are a visual formatting structure. It is difficult for a 
>screen reader user like myself to keep the blocks of code correctly 
>indented. Thus why I am using the comments at the end of the code.

Ah.

Speaking with some ignorance here, is it useful to you to indent using tab
characters instead of spaces, if you are using spaces? Can your screen
reader be asked to pronouns tabs differently? You would only need one tab
per indent.

Cheers,
Cameron Simpson <cs at cskk.id.au>



More information about the Tutor mailing list