python-how to copy file without overwrite destination file using python shutils ?

1. Purpose

In this post, I would demo how to copy file using python shutils , and would also demonstrate how to copy without overwrite the destination file.

2. The Environment

  • Python 3
  • Shutils

3. The code

3.1 How to copy file?

This is the demo that shows how to copy file from src to dest:

import os,sys,shutil

def copy_file(src_path, src_file_name, dest_path, dest_file_name):
    # construct the src path and file name
    src_path_file_name = os.path.join(src_path, src_file_name)

    # construct the dest path and file name
    dest_path_file_name = os.path.join(dest_path, dest_file_name)

    # do the real job
    shutil.copyfile(src_path_file_name, dest_path_file_name)
    print("copy from %s to %s ok" % (src_path_file_name,dest_path_file_name))
    pass

if __name__ == '__main__':
    src_path = sys.argv[1]
    src_file_name = sys.argv[2]
    dest_path = sys.argv[3]
    dest_file_name = sys.argv[4]

    copy_file(src_path,src_file_name,dest_path,dest_file_name)

3.2 Test the copy function

Suppose our working directory structure as follows:

.
└── working_directory/
    ├── copy_file_demo.py
    ├── logo.png
    └── images/
        └── readme.txt

Now we want to copy logo.png to images directory, and name it logo_bak.png, we do this job as follows:

$ python copy_file_demo.py . logo.png images logo_bak.png
copy from ./logo.png to images/logo_bak.png ok

After run the above command, we get this directory structure:

.
└── working_directory/
    ├── copy_file_demo.py
    ├── logo.png
    └── images/
        ├── readme.txt
        └── logo_bak.png

3.3 How to copy without overwritten

The code:

def copy_file_without_overwrite(src_path, src_file_name, dest_path, dest_file_name):
    # construct the src path and file name
    src_path_file_name = os.path.join(src_path, src_file_name)

    # construct the dest path and file name
    dest_path_file_name = os.path.join(dest_path, dest_file_name)

    # test if the dest file exists, if false, do the copy, or else abort the copy operation.
    if not os.path.exists(dest_path_file_name):
        shutil.copyfile(src_path_file_name, dest_path_file_name)
        print("copy from %s to %s ok" % (src_path_file_name, dest_path_file_name))
    else:
        print("already exist %s, copy aborted"%dest_path_file_name)

    pass

3.4 Test the copy again

Our directory is :

.
└── working_directory/
    ├── copy_file_demo.py
    ├── logo.png
    └── images/
        ├── readme.txt
        └── logo_bak.png

Then we execute the below command in the working directory:

$ python copy_file_demo.py . logo.png images logo_bak.png
already exist images/logo.png, copy aborted

It works!

4. About the shutil

Shutil module in Python provides many functions of high-level operations on files and collections of files. … This module helps in automating process of copying and removal of files and directories. shutil. copy() method in Python is used to copy the content of source file to destination file or directory.

Shutil is the abbreviation of shell utility, which implements advanced functions such as file copying, moving, compression, and decompression in Python. It is a Python system module and does not require additional installation.

The commonly used functions in shutil are listed below:

  • shutil.copyfile( src, dst) Copy from source src to dst. Of course, the premise is that the target address has writable permissions. The exception information thrown is IOException. If the current dst already exists, it will be overwritten
  • shutil.move( src, dst) move file or rename
  • shutil.copymode( src, dst) just copy its permissions and other things will not be copied
  • shutil.copystat( src, dst) copy permission, last access time, last modification time
  • shutil.copy( src, dst) copy a file to a file or a directory
  • shutil.copy2( src, dst) is copied on the basis of copy and the last access time and modification time of the file are also copied, similar to cp -p
  • shutil.copy2( src, dst) If the file systems in the two locations are the same, it is equivalent to a rename operation, just rename; if it is not in the same file system, it is a move operation
  • shutil.copytree( olddir, newdir, True/Flase) copies olddir to newdir. If the third parameter is True, the symbolic link under the folder will be kept when copying the directory. If the third parameter is False, it will Generate a physical copy in the copied directory to replace the symbolic link
  • shutil.rmtree( src) recursively delete a directory and all contents in the directory

5. Summary

In this post, we demonstrated how to use shutil in python to copy files from here to there. And we also demonstrated how to avoid overwriting the dest file when copying. Thanks for your reading. Regards.