Parsing problems: A journey from a text file to a directory tree

Neil Cerutti horpner at yahoo.com
Sun Sep 16 18:10:45 EDT 2007


On 2007-09-16, Martin M. <martinmichel at ame-electroplating.com> wrote:
> Hi everybody,
>
> Some of my colleagues want me to write a script for easy folder and
> subfolder creation on the Mac.
>
> The script is supposed to scan a text file containing directory trees
> in the following format:
>
> [New client]
>|-Invoices
>|-Offers
>|--Denied
>|--Accepted
>|-Delivery notes

Would it make sense to store it like this?

[('New client',
  [('Invoices', []),
   ('Offers', [('Denied', []), ('Accepted', [])]),
   ('Delivery notes', [])]]

> First I thought of an array like this:
>
> dirtreedb = {'New client': {'Invoices': {}, 'Offers': {'Denied': {},
> 'Accpeted': {}}, 'Delivery notes': {}}}

A dictionary approach is fine if it's OK for the directories to
be unordered, which doesn't appear to be the case.

> But this doesn't do the trick, as I also have to save the
> hierarchy level of the current folder as well...

The above does store the hierarchy, as the number of nesting
levels.

ditreedb['New Client']['Offers']['Denied']

-- 
Neil Cerutti



More information about the Python-list mailing list