How to move a file in Python

Moving a file changes its path and places the file in a different directory. For example, a file located at the path “directory1/file1.txt” can be moved to “directory2/file1.txt”.

Solution for How to move a file in Python : You can use shutil.move() to move a file Call shutil.move(src, dst) to move src to dst. Specify the entire path for both src and dst.

Other Solutions

Use pathlib.Path.rename() to move a file

  • If already using pathlib to work with files, this method can also be used. Renaming the path will move the file to a new directory.
  • Further reading: See documentation for pathlib here.


how-to-move-a-file-in-python