0

I have a list of file name(all files of the names listed here are present in a folder). I Want to move them to a new directory in my system. I want to do it through python. Please let me know the solution.

  • 1
    http://stackoverflow.com/questions/1868714/how-do-i-copy-an-entire-directory-of-files-into-an-existing-directory-using-pyth – logic Mar 23 '15 at 12:48
  • @logic: The question was about moving a list of files, not copying them. So this is a different question. – juhist Mar 23 '15 at 12:51
  • you can use `shutil` package that gives you flexibility to remove, move, delete files/directories in Python. – ha9u63a7 Mar 23 '15 at 12:58

2 Answers2

1
import shutil
import os

def Movetree(src, dst, symlinks=False, ignore=None):
    for item in os.listdir(src):
        s = os.path.join(src, item)
        d = os.path.join(dst, item)
        shutil.move(s, d)


Movetree ("D:\XSLT","D:\XSLT1")

user shutil and refer this : HERE

backtrack
  • 7,996
  • 5
  • 52
  • 99
0

Do you know the new directory to be in the same filesystem as the old directory?

If so, you should take a look at os.rename documentation at https://docs.python.org/2/library/os.html

I'm not doing the work for you, but you should use e.g. "/origdir/name1.txt" or "/origdir/name2.txt" as the src argument and "/newdir/name1.txt" or "/newdir/name2.txt" as the dst argument. So, you don't give the directory as the second argument; instead, you give the new name of the file within the directory.

If they may be on different filesystems, you need to copy the files and delete the originals. Or, you can use shutil.move explained at https://docs.python.org/2/library/shutil.html -- for shutil.move you may give the directory name as the second argument.

juhist
  • 4,210
  • 16
  • 33