{"id":5986,"date":"2021-05-16T10:43:31","date_gmt":"2021-05-16T05:13:31","guid":{"rendered":"https:\/\/python-programs.com\/?p=5986"},"modified":"2021-11-22T18:49:05","modified_gmt":"2021-11-22T13:19:05","slug":"python-how-to-move-files-and-directories","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-how-to-move-files-and-directories\/","title":{"rendered":"Python : How to move files and Directories ?"},"content":{"rendered":"
In python, It accepts source and destination path and moves file or directory from source pointed as src to destination pointed as dst.<\/p>\n \u00a0<\/strong>We will pass source file in first parameter and destination file in second parameter as string. Some points to keep in view.<\/p>\n \u00a0<\/strong>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.<\/p>\n \u00a0<\/strong><\/p>\n 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.<\/p>\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 <\/strong>We know that if there is no directory in a given path, then 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.<\/p>\n We can move an entire directory to other locations by keeping a view on some points.<\/p>\n <\/p>\n <\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" 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 : \u00a0We will pass source file in …<\/p>\nshutil<\/code> module offers various files related operations<\/p>\n
Syntax- shutil.move(src, dst)<\/pre>\n
Move a file to an another directory :<\/h3>\n
\n
FileNotFoundError<\/code>.<\/li>\n<\/ul>\n
import shutil\r\ncre_path = shutil.move('satya.txt', 'document') \r\nprint(cre_path)\r\n<\/pre>\n
Output :\r\nFileNotFoundError: [Errno 2] No such file or directory: 'saya.txt'<\/pre>\n
Move a file with a new name :<\/h3>\n
\n
import shutil\r\ncre_path = shutil.move('satya.txt', 'document\/sample.txt')\r\nprint(cre_path)<\/pre>\n
Output :\r\nFileNotFoundError: [Errno 2] No such file or directory: 'satya.txt'<\/pre>\n
Move all files in a directory to an another directory recursively :<\/h3>\n
import shutil,os,glob\r\ndef mov_every_Files_Dir(sourDir, destDir):\r\n print(sourDir)\r\n print(destDir)\r\n # checking if both the are directories are not\r\n if os.path.isdir(sourDir) and os.path.isdir(destDir) :\r\n # Iterate through all the files in source directory\r\n for filePath in glob.glob(sourDir + '\/*'):\r\n # moving the files to destination directory\r\n print(file_path)\r\n shutil.move(file_path, destDir);\r\n else:\r\n print(\"srcDir & dstDir should be Directories\") \r\ndef main(): \r\n if __name__ == '__main__':\r\n main()\r\n srcDir = '\/users\/sjones\/chem\/notes'\r\n desDir = '\/users\/sjones\/chem\/notes_backup'\r\n mov_every_Files_Dir(srcDir,desDir)\r\n<\/pre>\n
Move file and Create Intermediate directories :<\/h3>\n
shutil.move()<\/code> will give error. So we will create a function which move the file to destination directory and also create all directories in given path.<\/p>\n
import shutil, os, glob\r\ndef moven_cre_Dir(srcPath, destDir):\r\n if os.path.isdir(destDir) == False:\r\n os.makedirs(destDir); \r\n shutil.move(srcPath, destDir);\r\ndef main():\r\n if __name__ == '__main__':\r\n main()\r\n moven_cre_Dir(srcDir, destDir)\r\n srcFile = 'certificate\/document.txt'\r\n destDir = 'certificate\/document9'\r\n<\/pre>\n
Move symbolic links :<\/strong><\/h3>\n
Move a directory to an another directory :<\/h3>\n
import shutil\r\nsour_dir = 'satya'\r\ndest_dir = 'satya1'\r\nshutil.move(sour_dir, dest_dir)\r\n<\/pre>\n
Output :\r\nFileNotFoundError: [Errno 2] No such file or directory: 'satya'<\/pre>\n
\n