Python Script to Flatten a Directory structure

Hey Everyone,

I would like to create a python script to flatten a directory structure. This means, I have a bunch of karaoke tracks seperated into folders, I would like to have a python script that automatically moves all the files into one single directory, so that afterwords I can number those files from 1 to n.

Can somebody please given me a high level expanation of how this would be done (which libraries to use, etc) I will then try code it etc.

Thanks,
Ron

ronyosiAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

HonorGodSoftware EngineerCommented:
I would use os.walk() to traverse the specified directory structure, and process each file, in sequence, moving it to the target (destination) directory.

Here's an example of how to use os.walk()

http://xahlee.info/perl-python/traverse_dir.html
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
codefluxCommented:
Shell one-liner:

find INPUT_DIRECTORY -type f -exec mv {} OUTPUT_DIRECTORY \;

Open in new window


Replace INPUT_DIRECTORY and OUTPUT_DIRECTORY with correct values.
0
HonorGodSoftware EngineerCommented:
What happens with the one liner if the filename already exists?
0
Learn Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

codefluxCommented:
The file name will be overwritten.  You didn't say anything about duplicate file names. ;)
0
HonorGodSoftware EngineerCommented:
:-)   I didn't ask the original question.
0
codefluxCommented:
This will rename duplicate files to have ".$$" suffix - process id of the `sh` process.  Also assumes your file names have no " in them, and leaves some possibility that the process ids roll over and start creating duplicate files, but hey, it's a one-liner. ;)

find INPUT_DIR -type f -exec sh -c 'mv --backup --suffix=.$$ "'{}'" 'OUTPUT_DIR' ' \;

Open in new window

0
ronyosiAuthor Commented:
Thank you for your help :)
0
HonorGodSoftware EngineerCommented:
Thank you for the grade and points.

Good luck & have a great day.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Python

From novice to tech pro — start learning today.