Python move file to directory – Python : How to move files and Directories ?

How to move files and directories in python.

Python move file to directory: In python, shutil module offers various files related operations

Syntax- shutil.move(src, dst)

It accepts source and destination path and moves file or directory from source pointed as src to destination pointed as dst.

Move a file to an another directory :

Python move directory: We will pass source file in first parameter and destination file in second parameter as string. Some points to keep in view.

  • If there is no file in destination directory then a new file will be created with the same name.
  • If already exists a file with same name in destination directory, then the file will be overwritten.
  • If in destination path, the path is not valid not existed it will result in FileNotFoundError.
import shutil
cre_path = shutil.move('satya.txt', 'document')    
Output :
FileNotFoundError: [Errno 2] No such file or directory: 'saya.txt'

Move a file with a new name :

Python move files: If we pass a new name of file in destination path, it will reflect in source file as it will move to source file with new name.

  • If there was already existed file with same name, then the file will be overwritten.
  • If path doesn’t exist for destination path it will result in error.


import shutil
cre_path = shutil.move('satya.txt', 'document/sample.txt')
Output :
FileNotFoundError: [Errno 2] No such file or directory: 'satya.txt'

Move all files in a directory to an another directory recursively :

Move files python: Let there is a situation where we want move all the files form one directory to another directory. To implement this, using shtil.move(), we will iterate all files in source directory and move each file to destination directory.

import shutil,os,glob
def mov_every_Files_Dir(sourDir, destDir):
    # checking if both the are directories are not
    if os.path.isdir(sourDir) and os.path.isdir(destDir) :
        # Iterate through all the files in source directory
        for filePath in glob.glob(sourDir + '/*'):
            # moving the files to destination directory
            shutil.move(file_path, destDir);
        print("srcDir & dstDir should be Directories")   
def main():        
    if __name__ == '__main__':
    srcDir = '/users/sjones/chem/notes'
    desDir =  '/users/sjones/chem/notes_backup'

Move file and Create Intermediate directories :

                We know that if there is no directory in a given path, then shutil.move() will give error. So we will create a function which move the file to destination directory and also create all directories in given path.

import shutil, os, glob
def moven_cre_Dir(srcPath, destDir):
    if os.path.isdir(destDir) == False:
    shutil.move(srcPath, destDir);
def main():
    if __name__ == '__main__':
    moven_cre_Dir(srcDir, destDir)
    srcFile = 'certificate/document.txt'
    destDir =  'certificate/document9'

Move symbolic links :

If source file is a symbolic link, then a link will be created at destination path that will point to source link and subsequently source link will be deleted.

Move a directory to an another directory :

We can move an entire directory to other locations by keeping a view on some points.

import shutil
sour_dir = 'satya'
dest_dir =  'satya1'
shutil.move(sour_dir, dest_dir)
Output :
FileNotFoundError: [Errno 2] No such file or directory: 'satya'
  • If there was already a destination directory, then source directory will move to destination directory.
  • If destination directory is not there, then it will be created.
  • If there is no intermediate directory or path is invalid then there will be error.
  • If there is another directory with same name as that of the source in the destination directory, then there also will be error.