How to Copy File in Python

In this post we’ll see different ways you can copy a file in Python.

1. A simple way to copy a file in Python is to use read() method to read a file once you have a file object and then write the content to another file.

Following Python program opens an image file in binary mode and then writes it to another file.

def copy_file(src_file, dest_file):
    try:
        f1 = open(src_file, 'rb')
        f2 = open(dest_file, 'wb')
        b = f1.read()
        f2.write(b)
        print('Coying image completed...')
    finally:
        f1.close()
        f2.close()


#calling function
copy_file('F:/knpcode/image.png', 'F:/knpcode/Python/newimage.png')

2. Of course there are better ways in Python to copy a file. The shutil module offers a number of high-level operations on files and collections of files.
In that module there is a function shutil.copyfile(src, dest) which copies the contents of the file named src to a file named dst. Destination must be the complete target file name and it must be writable. If dst already exists, it will be replaced. Function returns the path to the destination file.

Note that this function doesn’t copy the metadata (permission bits, last access time, last modification time) of the file.

def copy_file(src_file, dest_file):
    try:
        shutil.copyfile(src_file, dest_file)
        print('Copying file completed...')
    except shutil.SameFileError as error:
        print(error)
        print('source and destination files are same')
    except IOError as error:
        print(error)
        print('Destination not writable')
    # Any other exception
    except:
        print('Error while copying')

3. If you want to copy file as well as metadata (permission bits, last access time, last modification time) then use shutil.copystat(src, dst)

def copy_file(src_file, dest_file):
    try:
        shutil.copystat(src_file, dest_file)
        print('Copying file completed...')
    except shutil.SameFileError as error:
        print(error)
        print('source and destination files are same')
    except IOError as error:
        print(error)
        print('Destination not writable')
    # Any other exception
    except:
        print('Error while copying')

4. If you want to copy a source file to another directory that can be done using shutil.copy(src, dst)

Here dst may specify a directory and the file will be copied into dst using the base filename from src. Function returns the path to the newly created file.

def copy_file(src_file, dest_dir):
    try:
        file_path = shutil.copy(src_file, dest_dir)
        print('Copying file completed...', dest_dir)
    except shutil.SameFileError as error:
        print(error)
        print('source and destination files are same')
    except IOError as error:
        print(error)
        print('Destination not writable')
    # Any other exception
    except:
        print('Error while copying')

Related Posts

That’s all for the topic How to Copy File in Python. If something is missing or you have something to share about the topic please write a comment.


You may also like

Share this post:

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.