python – Moving files to subdirectory programatically (without using unix mv command)

python – Moving files to subdirectory programatically (without using unix mv command)

I hope this could help:

import shutil,os,os.path


def walk(top):
    directory = None
    for root, dirs, files in os.walk(top, topdown=False):
        for name in dirs:
            directory = os.path.join(root, name)
        for name in files:
            f1 = os.path.join(root, name)
            if not f1.endswith(.py) and directory:
                shutil.move(f1,directory)


walk(os.path.realpath(os.curdir))

I tried it with this fs tree and it works:

-d/
  |-s/
  |-new file.txt
  |-new file1.txt
  |-move.py

python – Moving files to subdirectory programatically (without using unix mv command)

Leave a Reply

Your email address will not be published.