470,811 Members | 1,163 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,811 developers. It's quick & easy.

Re: recursive using the os.walk(path) from the os module

A. Joseph wrote:
I want to search through a directory and re-arrange all the files into e.g

All .doc files go into MS WORD folder, all .pdf files goes into PDF Folder.

I`m thinking of doing something with the os.walk(path) method from os
module, I need some ideal how the algorithm should look like, maybe
recursive ..any deal?
os.walk traverses the directory tree, so I'm not sure why you think that
your program needs to use recursion? wouldn't a plain loop work?

import os, shutil

for dirpath, dirnames, filenames in os.walk(directory):
for name in filenames:
source = os.path.join(dirpath, name)
... check extension and determine target directory ...
destination = os.path.join(targetdir, name)
shutil.move(source, destination)

tweak as necessary.


Sep 17 '08 #1
0 1411

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by Bryan | last post: by
15 posts views Thread by TaeKyon | last post: by
3 posts views Thread by rbt | last post: by
6 posts views Thread by rtilley | last post: by
2 posts views Thread by Martin Marcher | last post: by
12 posts views Thread by Lance Wynn | last post: by
reply views Thread by Simon Brunning | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.