How to move files and directories in python.
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 :
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') print(cre_path)
Output : FileNotFoundError: [Errno 2] No such file or directory: 'saya.txt'
Move a file with a new name :
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') print(cre_path)
Output : FileNotFoundError: [Errno 2] No such file or directory: 'satya.txt'
Move all files in a directory to an another directory recursively :
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): print(sourDir) print(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 print(file_path) shutil.move(file_path, destDir); else: print("srcDir & dstDir should be Directories") def main(): if __name__ == '__main__': main() srcDir = '/users/sjones/chem/notes' desDir = '/users/sjones/chem/notes_backup' mov_every_Files_Dir(srcDir,desDir)
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: os.makedirs(destDir); shutil.move(srcPath, destDir); def main(): if __name__ == '__main__': 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.