Back

Explore Courses Blog Tutorials Interview Questions
+1 vote
2 views
in Python by (3.5k points)
edited by

How to copy a file in python?

I already read this but found nothing.

2 Answers

0 votes
by (10.9k points)
edited by

@Ritik, You can use the copyfile() function present in shutil:

from shutil import copyfile

copyfile(file_name1, file_name2)

The function copyfile() will copy the contents of file_name1 into file_name2. If file_name2 already exists then it will get replaced, the location of file_name2 must be writable to avoid raising IOError exception.

Note: special files cannot be copied using this function.

0 votes
by (106k points)
edited by

By using the below-mentioned code you can copy a file in Python:-

import shutil

shutil.copy2('/src/dir/file.ext', '/dst/dir/newname.ext') 

shutil.copy2('/src/file.ext', '/dst/dir') 

You can use the following video tutorials to clear all your doubts:-

If you are looking for upskilling yourself in python you can join our Python Certification and learn from an industry expert.

Related questions

0 votes
2 answers
asked Aug 23, 2019 in Python by Sammy (47.6k points)
0 votes
1 answer
0 votes
1 answer
+3 votes
2 answers
0 votes
1 answer

Browse Categories

...