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)) 
			print file

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.

7 Years
Discussion Span
Last Post by shadwickman

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

-Google shutil.copytree.



:) 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.

This topic has been dead for over six months. 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.