All TalkersCode Topics

Follow TalkersCode On Social Media

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

Moving Files With Python

Last Updated : Aug 25, 2023

Moving Files With Python

In this article we will show you the solution of moving files with python, moving files with Workflows for file management & automation often require the use of Python. Python comes with a number of built-in modules & capabilities that simplify file coping with.

Shutil, which presents a high-level interface for report operations, is one such module.

You can relocate a report by using using the shutil.move() characteristic and provide the course to the original document in addition to the destination listing.

The record is copied to the new location, and its elimination from the source is dealt with by this function.

To transfer a document inside the same record system as an alternative, use the os.Rename() feature.

Without making a copy, this function only changes the file's location.

You can move files between folders or discs with ease using either approach. Now move to the concept of moving files with python.

Step By Step Guide On Moving Files With Python :-

import shutil
import os
def move_file(source_path, destination_dir):
    try:
        if os.path.exists(source_path):
            if os.path.exists(destination_dir):
                file_name = os.path.basename(source_path)
                destination_path = os.path.join(destination_dir, file_name)
                shutil.move(source_path, destination_path)
                print(f"File moved successfully from {source_path} to {destination_path}.")
            else:
                print(f"Destination directory '{destination_dir}' does not exist.")
        else:
            print(f"Source file '{source_path}' does not exist.")
    except Exception as e:
        print(f"An error occurred while moving the file: {str(e)}")
source_file_path = "/path/to/source/file.txt"
destination_directory = "/path/to/destination/"
move_file(source_file_path, destination_directory)
  1. The move_file() function in the shutil and os modules of Python allows us to move a file via a source path to a destination directory.
  2. We first make sure the source file is real.
  3. To make sure that the file is present at the specified source path, we utilize the os.path.exists() function.
  4. We warn that the source file was not discovered if it is not readily available.
  5. The destination directory's existence is then verified.
  6. To confirm that the supplied destination directory is genuine, we use the os.path.exists() function once more.
  7. A warning stating that it was unable to find the destination directory will show up if the directory is missing.
  8. If the destination directory and the source file are both present, the file is moved.
  9. We first take the file name out of the source path using os.path.basename().
  10. The extracted file name is then joined with the destination directory using os.path.join() to generate the destination path.
  11. This gives the whole path to the new place for the file.
  12. The shutil.move() function is then used to execute the actual file move operation.
  13. In response to receiving the source path and destination path as inputs, the shutil.move() function moves a file at a new location & deletes it from the old location.
  14. In the event that any issues arise while moving files, an exception will be raised.
  15. We are able to catch the exception and generate an error message detailing the issue we encountered by using a try-except block.

Conclusion :-

As a result, we were capable of understand the concept of transferring files using Python.

We additionally found that the use of Python to transport documents is a beneficial and powerful method to address document operations.

Python has robust modules like shutil & os that make transferring files easier.

I hope this article on moving files with python helps you and the steps and method mentioned above are easy to follow and implement.