In this article we will discuss different ways to move files and directories in python.

In python we have a shutil module that provides various files related operations.  To import the shutil module use following line,

It also provides functions to move files i.e.

It accepts source and destination path as string and moves the source file/directory pointed by src to the destination location pointed by dst and returns the destination path.

Both destination and source paths can be relative or absolute. Let’s use this to move files and directories,

Move a file to an another directory

Pass the source file path as string in first parameter and destination directory path as string in second parameter,

it will move the file to that directory and returns the path of moved file as string i.e.

Some Points to remember:

  • If destination directory doesn’t exists then it will create a file with that name.
  • If there was already a file with same name in destination directory, then it will be overwritten.
  • If in destination path any intermediate directory does not exists or path is not valid then it will cause an error,
    • FileNotFoundError: [Errno 2] No such file or directory: ‘test3/test/’

Move a file with a new name

In the destination path we can also pass the new name of file, it will move the source file to that location with new name i.e.

Some points to remember:

  • If there was already a file at with that name, then it will be silently overwritten.
  • If any of the intermediate directory in destination path doesn’t exists then it will cause error.

Move all files in a directory to an another directory recursively

Suppose we want to move all the files in a directory to another directory. For that we need to iterate over all the files in source directory and move each file to destination directory using shutil.move() i.e.

Let’s use this to move all files in boost_1_66 to boost_1_66_backup i.e.

Move file and Create Intermediate directories

As we know that shutil.move() will give error if any of the intermediate directory is not present then it will give error. So, let’s create a function that will move the file to destination directory and will also create all directories in the given path i.e.

Let’s use this to move a file to a path that don’t exists i.e.

Move symbolic links

In case source file path i.e. src is a symbolic link then at destination path a link will be created that will point to target of source link. Also source link will be deleted.

Move a directory to an another directory

We can also move a complete directory to another location i.e.

Some points :

  • If destination directory exists then source directory will be moved inside that.
  • If destination directory doesn’t exists then it will be created.
  • If any intermediate directory doesn’t exists i.e. path is not valid then it can cause error.
  • If destination directory already contains an another directory with same name as source directory then it will cause error.

Complete example is as follows,

 

 

Click Here to Subscribe for more Articles / Tutorials like this.