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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Fredrik Lundh

    #1

    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(directo ry):
    for name in filenames:
    source = os.path.join(di rpath, name)
    ... check extension and determine target directory ...
    destination = os.path.join(ta rgetdir, name)
    shutil.move(sou rce, destination)

    tweak as necessary.

    </F>

Working...