I just learned about dictionaries in Python and tried to implement them into a program I developed.

The folders in the dictionary absolutely MUST be created in the listed order. But if for whatever reason they cannot be created, I want the name I specify to be printed instead of the entire directory name, like it did when I a list in this section of code. If that doesn't make much sense you'll see what I mean in the code...I hope.

Using a dictionary would be perfect for doing what I want, except it automatically sorts the contents! This totally breaks the program! :confused:

FrameFolders = {modsDir:"Mods", Packages:"Packages", DCCache:"DCCache"}

for path in FrameFolders:
if not os.path.isdir(path):
Ok = False
print "%s folder does not exist. Creating..." % FrameFolders[path]
#Just using % path prints the ENTIRE directory name
os.mkdir(path)

if os.path.isdir(path): print "Folder sucessfully created.\n"
else: raise NotFoundError("Could not create %s folder!" % FrameFolders[path])

Is there some workaround or something I can use in the place of a dictionary to get the results I want?

3
Contributors
5
Replies
6
Views
6 Years
Discussion Span
Last Post by pyTony

First answer is not to reinvent the wheel: use os.makedirs():

>>> import os
>>> os.chdir('d:\\tony')
>>> os.makedirs(os.path.join('create','all','these','dirs'))
>>> os.system('ls -R create && pause')
create:
all

create/all:
these

create/all/these:
dirs

create/all/these/dirs:

If you want to implement it as practise, use normal pathname, split it from the path separators to a list and check each part if they exist and create them when needed.

The sequentiality could be faced with dictionary if you really want it but it would be wrong data structure choice (there exist in newest versions also an ordered dictionary). I can not think reason not to use string and list from that.

Edited by pyTony: n/a

I figured this out, thanks. But what do you mean by

First answer is not to reinvent the wheel: use os.makedirs():

Why use that instead of the code I already have?

You can use a list

FrameFolders = [(modsDir,"Mods"), (Packages,"Packages"), (DCCache,"DCCache")]

for path, name in FrameFolders:
if not os.path.isdir(path):
Ok = False
print "%s folder does not exist. Creating..." % name
#Just using % path prints the ENTIRE directory name
os.mkdir(path)

if os.path.isdir(path): print "Folder %s sucessfully created.\n" % name
else: raise NotFoundError("Could not create %s folder!" % name)

Huh...much easier than what I came up with! I learned something. Thank you.

I figured this out, thanks. But what do you mean by

Why use that instead of the code I already have?

os.makedirs is already tested code, generally it is not good practise to do again something that is standard library stuff like doing series of hierarchical directories.

There is often so called "corner cases" which are found in full testing, but you are not investing enough testing effort when you do it yourself. Then the user inputs the 'strange value' and your program crashes, when fully tested code gives sensible error message.

Maybe I did not understand fully the purpose of your mkdirs, did you intend to create hierarchical directories, or what?

Edited by pyTony: n/a

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.