Create lists of directories and files

I've written a program and outside of that program I want to make a new python program which generates lists (or in some cases maybe dictionaries) from certain directories and the files in them.

I've puzzled around with the os.path(), os.chdir(), os.getcwd() functions, the glob.glob() function and I've tried to see how os.walk() would work.

Say I have the following file structure:

I have a folder named "Library", with the subfolders "Detached", "Semi_Detached" and "Appartments".
In each subfolder it then has a certain amount of files like:
"1_simple.max", "1_detailed.max", "1.csv", "2_simple.max", "2_detailed.max", "2.csv" and "x_simple.max", "x_detailed.max", "x.csv",

First, the directory structure shown by walk doesn't quite appear to match what you said the structure would be.

For example, the directories under Library appear to be prefixed with a number (as in 1_Vrijstaand).

For another example, the files under the individual directories don't show the 1_simple.max, 1_detailed.max, 1.csv structure you indicated, but just 1.max and 1.csv

Are the 1_simple.max and 1_detailed.max going to exist?

Will it always be 'simple' and 'detailed' or might there be other words between "1_" and ".max" that you have to collect as well.

I personally, try not to change directories once my application is started. I'm not against you doing it if it makes sense, but as you passed an absolute directory to os.walk I don't suspect it was required.

I'm going to recommend that you create some classes to hold more of your data and then you can fill the classes as you either walk the libaray or use an alternative iteration.

I'm thinking an instance of a class for each subdirectory under Library. Then inside that class intance, you could create an instance of another class for each csv file you find in the subdirectory. That class would also store the names of all of max files that match the csv name. (You will need to be careful to make sure that you don't put '10_simple.max' under the class for '1.csv'.) Note that this is not quite the structure that you proposed, but it would seem to make more sense based on the data available. (Feel free to ignore this part or tell me I'm wrong.)

An alternative to os.walk for creating the structure might be os.listdir.

You could do something like:

librarypath = "G:\\Afstuderen\\Library"
for libname in os.listdir(librarypath):
pathname = os.path.join(librarypath, libname)
if os.path.isdir(pathname):
# create the class for the subdirectory here
# one option for finding the files inside the subdirectory
for fname in os.listdir(pathname):
fullname = os.path.join(pathname, fname)
base,ext = os.path.splitext(fullname)
if ext == ".csv":
# create the class for the csv here
# we could either look for the .max files now,
# or come back later and look for them...
# second option for finding the files in the subdirectory
for csvname in glob.glob(os.path.join(pathname, "*.csv")):
# create the class for the csv here

[/code] tags, but if you use the language specific tags [code=python]
[/code] for example, you get line numbers and syntax highlighting as well. The syntax highlighting makes the code easier to read and the line numbers give you a point of reference if you need to talk about just part of the code.