Back

Explore Courses Blog Tutorials Interview Questions
0 votes
3 views
in Python by (47.6k points)

How do I copy a file in Python?

I couldn't find anything under os.

2 Answers

0 votes
by (106k points)
edited by

For copying a file in Python you can use the shutil library which has many methods which you can use. 

One of them is as follows:-

from shutil import 

copyfile copyfile(src, dst)

To know more about this you can have a look at the following video tutorial:-

0 votes
by (108k points)

Instead of using copy file(src,dst), you can use the copy2(src,dst) as it allows dst to be a directory by default instead of mentioning the complete target file name, and in that the basename of src is generally used for forming the new file.

Refer to the below example:

import shutil

shutil.copy2('/src/dir/file.ext', '/dst/dir/newname.ext') # complete target filename given

shutil.copy2('/src/file.ext', '/dst/dir') # target filename is /dst/dir/file.ext

If you want to know more about Python, then do refer to the Python certification course that will help you out in a better way. 

Related questions

0 votes
1 answer
+1 vote
2 answers
asked May 30, 2019 in Python by Ritik (3.5k points)
0 votes
1 answer
0 votes
1 answer
+1 vote
3 answers
asked May 21, 2019 in Python by Shivangi (13.2k points)

Browse Categories

...