All TalkersCode Topics

Follow TalkersCode On Social Media

devloprr.com - A Social Media Network for developers Join Now ➔

Python Move File To Another Directory

Last Updated : Mar 11, 2024

Python Move File To Another Directory

In this article we will show you the solution of python move file to another directory, moving a file from one location to another in Python needs a few steps when working with file operations.

You must first import the necessary module, such as shutil, which includes functions for working with files.

The shutil.move() method is a strong tool that you may use to efficiently carry out the move operation.

You can access a variety of tools and settings for handling files by utilising this function.

The source file path & the destination directory path are the two inputs that the shutil.move() function expects.

The location of the file you want to move is indicated by the source file path, and the target directory is indicated by the destination directory path.

You may easily move files between directories, partitions, and even devices owing to this versatility. Now move to the concept of python move file to another directory.

Step By Step Guide On Python Move File To Another Directory :-

import os
import shutil
def move_file(source_file, destination_directory):
    if os.path.exists(source_file) and os.path.isdir(destination_directory):
        filename = os.path.basename(source_file)
        destination_path = os.path.join(destination_directory, filename)
        shutil.move(source_file, destination_path)
        print("File moved successfully.")
    else:
        print("Either the source file doesn't exist or the destination directory is invalid.")
# Example usage
source_file = 'path/to/source/file.txt'
destination_directory = 'path/to/destination/directory'
move_file(source_file, destination_directory)
  1. A file can be moved easily from a source directory to a destination directory using the Python method move_file().
  2. We import the os and shutil modules first.
  3. While the shutil module supports practical file operations, the os module delivers features relating to operating system interactions.
  4. The move_file() function, which requires the source_file and destination_directory parameters, is then defined.
  5. A file will be moved using this function from the source file path to the designated destination directory.
  6. Before starting the file move operation, we do a few checks inside the code.
  7. The os.path.exists() and os.path.isdir() functions are used to check for the existence of the source file & the destination directory, respectively.
  8. If these conditions meet the criteria, the file migration process is started.
  9. We may extract the filename from the source file path using os.path.basename().
  10. The complete destination path for the file is then created by combining the filename and destination directory paths using os.path.join().
  11. After finding out the destination path, we use shutil.move() to move the file from the source to the destination.
  12. The source file path and the destination file path are the only two arguments this function accepts to handle file transfers.
  13. If the file move method is successful, we print a message indicating the file's relocation.
  14. However, if the source file does not exist or the destination directory is incorrect, we display an appropriate error message to warn the user.

Conclusion :-

As a result, we have successfully learned how to move files between directories using Python.

We also found that the shutil.move() function makes moving files between folders in Python quick and safe.

The shutil.move() method offers error handling capabilities while expediting file transfers.

It can deal with a wide range of situations, including renaming files with conflicting names.

I hope this article on python move file to another directory helps you and the steps and method mentioned above are easy to follow and implement.

Author Image About Riya

A recent graduate with a Bachelor of Technology (B.Tech) in Computer Science from India. She is passionate about leveraging technology to solve real-world problems. With a strong foundation and experience in programming languages such as Python, Django, HTML, CSS, and JavaScript, java, php and have honed her skills through hands-on projects and coursework.

Follow Riya On Linkedin 🡪