EDUCBA Logo

EDUCBA

MENUMENU
  • Explore
    • EDUCBA Pro
    • PRO Bundles
    • Featured Skills
    • New & Trending
    • Fresh Entries
    • Finance
    • Data Science
    • Programming and Dev
    • Excel
    • Marketing
    • HR
    • PDP
    • VFX and Design
    • Project Management
    • Exam Prep
    • All Courses
  • Blog
  • Enterprise
  • Free Courses
  • Log in
  • Sign Up
Home Software Development Software Development Tutorials File Handling Python Tutorial Python Copy File
 

Python Copy File

Python Copy File

Introduction to Python Copy File

Python copy file function is a high-level operation in python. It is used to copy files from one location to another location. Python interacts with the operating system to perform this operation, and before carrying this operation, we need to import os module and shutil modules; without these modules, this operation cannot be done. We can copy single files and multiple files present in the directory. Though these functions are high-level operations, still they don’t copy metadata, owner information, ACLs, and alternate data streams are not copied.  Source and destination paths are like objects or string.

 

 

Destination file path names should be proper and according to the rules of the copy function. For example, the source and destination file path should not be the same; otherwise, python will generate the same file error.

Watch our Demo Courses and Videos

Valuation, Hadoop, Excel, Mobile Apps, Web Development & many more.

Syntax

shutil.copyfile(src, dst, *, follow_symlinks=True)
  • src: complete source file, and it should be in string format. This parameter is mandatory.
  • dst: destination file path, and it should be also in string format and not the same as the source. This parameter is mandatory.
  • follow_symlinks: This is the optional parameter, and the default value is true. If the value is false and it is symbolic links, then the file will not be copied; instead, a new symbolic link is created.

The destination file path should be writable; if the directory does not have write permission, then python will return OSError. This function does not allow copying special files like pipes, characters, or block devices.

Examples of Python Copy File

Given below are the examples of the Python Copy File:

Example#1

Code:

import os
import shutil
shutil.copy('Python/original.txt', 'Python/duplicate.txt')

Output: 

Python Copy File output 1

This is a very basic example of shutil.copy function. We have imported the os and shutil library. In the shutil.copy function, we have specified the first parameter as a source file, and the second parameter is the destination file. In this case, the source and destination folder are the same, but file names are different so that they will be successfully copied.

Example #2

Code:

import os
import shutil
dir = '/Users\Big Data Cluster\Documents\Python\Testing'
Source = '/Users\Big Data Cluster\Documents\Python\Testing\Sample.txt'
Destination = '/Users\Big Data Cluster\Documents\Python\Testing\Test.txt'
print(os.listdir(dir))
shutil.copy(Source,Destination)
print(os.listdir(dir))

Output:

Python Copy File output 2

In the above program, we have imported the required library. We have specified the directory path in the dir variable. We have stored the source file path in the source variable and the destination file path in the destination variable. Finally, we have passed the source and destination variables in the shutil.copy function.

We have the os.listdir method to list all the files that are present in the directory. As we can see that before the shutil.copy method, there was one file, ‘Sample.txt’ inside the directory, and after the function was executed file was copied and named as ‘Test.txt.’

Example #3

Code:

import os
import shutil
Source = '/Users\Big Data Cluster\Documents\Python\Testing\Sample.txt'
Destination = '/Users\Big Data Cluster\Documents\Python\Testing\Test.txt'
shutil.copy(Source,Destination)

Output: 

output 3

In the above example, we have created two variables, source and destination holding paths for source and destination files. But in this example, as you can see, we have specified the same file path in both source and destination. In this shutil.copy function will return the same file error, as you can see in the above screenshot.

Conclusion

Python provides a shutil.copy method to copy our file from one location to another location. This is a very easy and useful method for handling files; the only drawback is that it doesn’t copy the file’s metadata information.

Recommended Articles

This is a guide to Python Copy File. Here we discuss the Examples of the Python Copy File along with the syntax, codes, and outputs. You may also have a look at the following articles to learn more –

  1. Python User Input
  2. Python Return Value
  3. Max Function in Python
  4. Python Variable Scope

Primary Sidebar

Footer

Follow us!
  • EDUCBA FacebookEDUCBA TwitterEDUCBA LinkedINEDUCBA Instagram
  • EDUCBA YoutubeEDUCBA CourseraEDUCBA Udemy
APPS
EDUCBA Android AppEDUCBA iOS App
Blog
  • Blog
  • Free Tutorials
  • About us
  • Contact us
  • Log in
Courses
  • Enterprise Solutions
  • Free Courses
  • Explore Programs
  • All Courses
  • All in One Bundles
  • Sign up
Email
  • [email protected]

ISO 10004:2018 & ISO 9001:2015 Certified

© 2025 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

EDUCBA
Free Software Development Course

Web development, programming languages, Software testing & others

By continuing above step, you agree to our Terms of Use and Privacy Policy.
*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more

🚀 Limited Time Offer! - ENROLL NOW