Move Files in Python

Learn via video course
FREE
View all courses
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Python Course for Beginners With Certification: Mastering the Essentials
Python Course for Beginners With Certification: Mastering the Essentials
by Rahul Janghu
1000
4.90
Start Learning
Topics Covered

Overview

In Python move file refers to moving a file/files or folder with a certain number of files from the source location to the target location. Though we can simply drag-drop or copy and paste and move the file. But in Python move file can happen automatically via the simple methods (shutil.move(), os.rename() as well as pathlib.path().rename() functions) that we shall study in the below article.

Introduction to How to Move Files in Python

While working in Python, there arise scenarios where we need Python to move files from the source path to its target path. We usually copy these files or drag and drop the source folder with all the files to the destination location as per our scenarios.

As we proceed along the article, we shall find various ways in which we can efficiently move file in Python from the source location to their destination location. We can move file in Python ( covering scenarios like moving a single file, all files from a folder, or only those file that match a certain pattern from the source location to the target location).

We can do the move file in Python by simply using any of the three methods that we shall be learning. So next time you have a chance at any of your messy folders and may want to extract single, all, or match a certain pattern file and move file in Python shall come in handy for you.

Method 1: Using the shutil.move() Function

Intro:

With the shutil module, we shall be using its move() function that provides a simple and easy way to move file in Python. Also, with the OS module in Python, move file in Python become a lot easier as this module offers interactions with their operating systems. By, the shutil.move() method we can efficiently move file in Python as this syntax takes two arguments where the first one is the absolute source path while the second one is the target path. In these paths, we can either specify the file or folder name in Python to move file. The move() function will then softly move the file from the source path to the target path.

The shutil.move() function works by creating a copy of the defined file with the path from the source path. Then it stores this copy in the target location path. Finally, once the copy is successfully created, Python deletes the original file located at source_path.

QuickNote: The shutil module offers many functions for the high-level operations that can be performed on individual files and their file collections.

Syntax:

The syntax we use to move file in Python via the shutil method is given below:

Parameters:

With the shutil.move() function in Python to move file, we can efficiently move file in Python as this syntax takes two parameters s where the first one is the absolute source path while the second one is the target path.

Return Value:

The return value we obtain by using a shutil.move() function in Python to move file is that the file stored in the source path is efficiently moved to the target location without any hassle of coding lines of code.

Output:

Algorithm:

  • We analyze three scenarios here, where in case 1 we move a single file from the source path to the target path, in case 2 we move all files from the source path to the target path and in case 3 we move files matching specific pattern from source path to the target path.
  • We then define an absolute source location and target location in source_path and target_path variables respectively.
  • For case1, we use the shutil.move() syntax in Python to move the file from the source location to the target location.
  • For case 2, we first collect all files from the source path using the os module listdir function. Then we use the for loop in Python to iterate through each file gathered in the collect_allfiles variable which is the move file in Python.
  • For case 3, we gather all files from the source path using the glob module. And from the os module, the path.join() function is used to join one or more path components intelligently where we specify the required pattern that must be found in the file name that we want to move. Then we use the for loop in Python to iterate through each file gathered in the collect_allfiles variable which is the move file in Python.

QuickNote: Always ensure that the ‘r‘ character is placed before the paths to avoid encountering the following error:

Code:

Output:

All files in the Source folder: SHUTIL SOURCE FOLDER

Case 1: Move a Single File from the Source path to the Target path SHUTIL SINGLE FILE Case 2: Move all Files from the Source path to the Target path PYTHON SHUTIL TARGET FOLDER Case 3: Move Files Matching specific patterns from the source path to the target path MOVE FILES MATCHING SPECIFIC PATTERNS

Explanation: As seen above, as per case 1, only a single file is moved from the source location to the target location successfully, for case 2, all the files from the source folder are successfully moved to the target folder. While for case 3, we picked up all. the files matching the defined pattern and move them to the target folder.

Method 2 : Using pathlib.Path().rename()

Intro:

Another approach in Python to move a file is a more object-oriented approach with the help of the pathlib module. With the pathlib module, we can manage various files and dictionaries, and is a common module in Python for providing an object to do so. The path object is created under the Path() function, where this path is defined as the name of the main object which is used to work with files. Also, we make the changes to the path of the object via the rename() method.

Syntax:

The syntax we use in Python to move file from the source location to the target location is given below:

Parameters:

The pathlib.Path(').rename() function in Python to move files as this syntax takes two parameters where the first one is the path() function while the second one is the rename() function. In the path() function, the path is the name of the main object which is used to work with files. The rename() function, is used to make the changes to the path of the object, which is the target location.

Return Value: The return value we obtain by using a pathlib.path().rename() function in Python to move files is that the file stored in the source path is efficiently moved to the target location without any hassle of coding lines of code.

Output:

Algorithm:

  • We analyze three scenarios here, where in case 1 we move a single file from the source path to the target path, in case 2 we move all files from the source path to the target path and in case 3 we move files matching specific pattern from source path to the target path.
  • We then define an absolute source location and target location in source_path and target_path variables respectively.
  • For case1, we use the pathlib.path().rename() syntax in Python to move the file from the source location to the target location.
  • For case 2, we first collect all files from the source path using the os module listdir function. Then we use the for loop in Python to iterate through each file gathered in the collect_allfiles variable which is the move file in Python.
  • For case 3, we gather all files from the source path using the glob module. And from the os module, the path.join() function is used to join one or more path components intelligently where we specify the required pattern that must be found in the file name that we want to move. Then we use the for loop in Python to iterate through each file gathered in the collect_allfiles variable which is the move file in Python.

Code:

Output:

All files in the Source folder: PATHLIB SOURCE FOLDER

Case 1: Move a Single File from the Source path to the Target path PATHLIB SINGLE FILE Case 2: Move all Files from the Source path to the Target path PATHLIB TARGET FOLDER Case 3: Move Files Matching specific patterns from the source path to the target path PATHLIB MOVE FILES MATCHING SPECIFIC PATTERNS

Explanation: As seen above, as per case 1, only a single file is moved from the source location to the target location successfully, for case 2, all the files from the source folder are successfully moved to the target folder. While for case 3, we picked up all. the files matching the defined pattern and move them to the target folder.

Method 3 os.rename() Method Move Files in Python

Intro: Another way in Python to move files, we can implement the rename() function from the os module. This os.rename() function works a little differently as compared with the shutil.move() function. This rename() function is widely used to relocate the files from the source location. the target location. With the rename() function, you can simply change the folder name of the files and the rename function understands that it needs to move the files to that new folder. Here, no copying of the files into the new location happens as in the previous method studied so far, the rename() function simply alters the path of the file from the source to the target folder location.

Syntax:

The syntax we use for to os.rename() function in Python to move files is given below:

Parameters: The os.rename() function in Python to move file as this syntax takes two parameters where the first one is the source path and the second one is the target path. With the rename() function, the file from the source path is moved or the target location path is simply altered without any copying of the files involved.

Return Value: The return value we obtain by using an os.rename() function in Python to move files is that the file stored in the source path is efficiently moved to the target location without any hassle of coding lines of code.

Output:

Algorithm:

  • We analyze three scenarios here, where in case 1 we move a single file from the source path to the target path, in case 2 we move all files from the source path to the target path and in case 3 we move files matching specific pattern from source path to the target path.
  • We then define an absolute source location and target location in source_path and target_path variables respectively.
  • For case1, we use the os.rename() syntax in Python to move the file from the source location to the target location.
  • For case 2, we first collect all files from the source path using the os module listdir function. Then we use the for loop in Python to iterate through each file gathered in the collect_allfiles variable which is the move file in Python.
  • For case 3, we gather all files from the source path using the glob module. And from the os module, the path.join() function is used to join one or more path components intelligently where we specify the required pattern that must be found in the file name that we want to move. Then we use the for loop in Python to iterate through each file gathered in the collect_allfiles variable which is the move file in Python.

Code:

Output:

All files in the Source folder: RENAME SOURCE FOLDER

Case 1: Move a Single File from the Source path to the Target path MOVE A SINGLE FILE Case 2: Move all Files from the Source path to the Target path MOVE ALL FILES Case 3: Move Files Matching specific patterns from the source path to the target path SPECIFIC PATTERNS

Explanation: As seen above, as per case 1, only a single file is moved from the source location to the target location successfully, for case 2, all the files from the source folder are successfully moved to the target folder. While for case 3, we picked up all. the files matching the defined pattern and move them to the target folder.

Conclusion

  • Moving a file/files or folder with a certain number of files from the source location to the target location. Though we can simply drag-drop or copy and paste and move the file. But, in Python moving files can happen automatically via the simple methods (shutil.move(), os.rename() as well as pathlib.path().rename() functions)

  • The shutil.move() function works by creating a copy of the defined file with the path from the source path. Then it stores this copy in the target location path. Finally, once the copy is successfully created, Python deletes the original file located at source_path.

  • The rename() function simply alters the path of the file from the source to the target folder location whereas with shutil.move() all files are copied and then moved to the target location.