VOOZH about

URL: https://www.geeksforgeeks.org/python/copy-files-and-rename-in-python/

⇱ Copy Files and Rename in Python - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Copy Files and Rename in Python

Last Updated : 23 Jul, 2025

Copying and renaming files is a common task in programming, and Python provides several ways to accomplish this efficiently. In this article, we will explore some commonly used methods for copying files and renaming them: using the shutil module's copy() and pathlib module's Path class. Each method has its advantages, and choosing the right one depends on the specific requirements of your project.

How To Copy Files And Rename Them In Python?

Below, are the generally used method for How To Copy Files And Rename Them In Python.

Copy Files And Rename using shutil.copy()

The shutil module in Python is a powerful utility for file operations, including copying files. The copy() function from this module allows us to copy a file from one location to another. Here's a simple example: This example uses the shutil.copy() method to copy the file and shutil.move() to rename the copied file in the destination folder.

Output

Successfully Created File and Rename

Copy Files And Rename Using pathlib.Path

The pathlib module was introduced in Python 3.4 to provide an object-oriented interface for file system paths. The Path class includes the rename() method, making it convenient for both copying and renaming files: In this example, the Path class is used to create path objects, and rename() is employed to both copy and rename the file in the destination folder.

Output :

Successfully Created File and Rename
Comment