Hi there, this is my first python project. I'm new to python but I have experience in a few other languages.

I'm trying to export (to mysql) a list of files and subdirectories from several directories on my hard drive. Basically just so I can have an online database of the files on my hard drive.

Here is my recursive directory walker:

def getlist(path):
	for file in os.listdir(path):
		if os.path.isdir(os.path.join(path, file)):
			print os.path.join(path, file)
			getlist(os.path.join(path, file)) 
		else:
			print file
	return

I was using os.walk beforehand, but couldn't figure out how to go dir->files dir->files, instead it was going dir->dir and THEN dir->files, if you understand.

Now I need some way to be able to keep the directory/file hierarchy while exporting to a mysql db. The only thing I could think of was for each dir to have an id but then I couldn't figure out what to do after that. Once it's done importing to the db, I will make a php/js page that will have a fancy display of all my folders/files. It needs to stay in it's current hierarchy because I will have collapsiblocks inside collapsiblocks (ie: files inside a dir inside a dir)

Thanks for any and all help.

You sounds like you've got it together, so the simple explanation is:

-Google shutil.copytree.

-Modify.

-Execute.

:) If you want a complete solution I can talk more about this, but I figured you might just want a poke in the right direction.

os.walk is useful, but kinda tough to bend in the direction you want because you have to deal with callbacks, and sometimes that's not all the info you need to pass, copytree is way awesome. On the python doc page they have an example of it, I copy pasted, modified and bended it to my will. Heh heh.

Like zachabesh said, you should take a look at the shutil module for more file operation functions. And copytree may be of particular interest to you too :P

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.