+1 vote
1 view
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 (107k points)

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:-

Related questions

0 votes
2 answers
asked Aug 23, 2019 in Python by Sammy (47.8k points)
0 votes
1 answer
0 votes
1 answer
+3 votes
2 answers
0 votes
1 answer
Welcome to Intellipaat Community. Get your technical queries answered by top developers !


Categories

...