HomeLinuxTips on how to Copy a File in Python

Tips on how to Copy a File in Python


Copying a file is vital in Python whenever you need to make a file backup earlier than modifying it using your script, or whenever you need to transfer and rename a file to a different location. Python supplies numerous modules and features to repeat a file in Python together with the “shutil” module.

This Python tutorial supplies numerous methods to repeat a file in Python. A number of the strategies are listed beneath:

Technique 1: Copy a File in Python Utilizing “shutil.copyfile()” Technique

In Python, the “shutil.copyfile()” methodology copies the contents (excluding metadata) of the unique file to the goal file and returns the vacation spot/goal file.

Syntax

shutil.copyfile(src, dst, *, follow_symlinks=True)

 

Within the above syntax, “src” is the unique/supply file path, and “dst” refers back to the vacation spot/closing file path. The “follow_symlinks” parameter is non-compulsory and its default worth is “True”. It would create a symbolic hyperlink as a substitute of copying the file whether it is set to “False“.

The beneath snippet reveals the unique file location together with its title:

Instance

Right here is an instance code:

import shutil
source_file = r‘C:UserspDocumentsprogramfilename.txt’
destin_file = r‘C:UserspDownloadsSoftwarefilename.txt’
shutil.copyfile(source_file, destin_file)

 

Within the above code, the “shutil.copyfile()” methodology copies the file by taking the unique/supply file and vacation spot file areas as their arguments, respectively.

Output

The above snippet verified that the precise file has been copied from one location to a different.

Technique 2: Copy a File in Python Utilizing “shutil.copy()” Technique

The “shutil.copy()” methodology is utilized to repeat the information of the unique file to the goal file. Nonetheless, the metadata of the file shouldn’t be copied.

shutil.copy(src, dst, *, follow_symlinks=True)

 

Instance

The beneath code is used to repeat the file in Python:

import shutil
source_file = r‘C:UserspDocumentsprogramfilename.txt’
destin_file = r‘C:UserspDownloadsSoftwarefilename.txt’
shutil.copy(source_file, destin_file)

 

Within the above code strains, the “shutil.copy()” methodology takes the supply/src and vacation spot/dst file with full areas as their arguments and copies the file accordingly.

Output

In line with the above snippet, the precise file has been copied from one location to a different appropriately.

Technique 3: Copy a File in Python Utilizing the “learn()” and “write()” Features

The “learn()” and “write()” features are used together with the “open()” perform in learn and write mode to repeat the file from one location to a different in Python.

Instance

The below-stated code is utilized to switch information between totally different paths:

with open(r‘C:UserspDocumentsprogramfilename.txt’, ‘r’) as sf:
    with open(r‘C:UserspDownloadsSoftwarefilename.txt’, ‘w’) as df:
        df.write(sf.learn())

 

Within the above code:

  • Firstly, we open a supply file named “txt” utilizing the “open()” perform.
  • After that, one other file “txt” is created utilizing the “open()” perform within the vacation spot folder.
  • The “learn()” perform reads all the pieces from the given supply file and writes it to the vacation spot file utilizing the “write()” perform.

Output

This snippet reveals that the file was efficiently copied.

Technique 4: Copy a File in Python Utilizing the “shutil.copyfileobj()” Technique

The “shutil.copyfileobj()” methodology is utilized to repeat the data of a file-like object to a distinct file-like object. By default, this methodology copies information in “64 KB” chunks, however you may specify a distinct buffer dimension utilizing the “bufsize” argument.

Syntax

shutil.copyfileobj(fsrc, fdst[, length])

 

Within the above syntax, “fsrc” is a file-like object representing the supply file to be copied, and “fdst” is a file-like object representing the vacation spot file the place “fsrc” will likely be copied. The “non-compulsory” parameter size denotes buffer dimension.

Instance

The beneath code is utilized to repeat the file in Python:

import shutil
source_file = open(r‘C:UserspDocumentsprogramfilename.txt’, ‘rb’)
destin_file = open(r‘C:UserspDownloadsSoftwarefilename.txt’, ‘wb’)
shutil.copyfileobj(source_file, destin_file)

 

On this code, the “shutil.copyfileobj()” methodology is utilized to repeat the file from the supply/authentic file to the vacation spot file.

Output

The above snippet verified that the actual textual content file has been copied from supply to vacation spot.

Conclusion

To repeat a file numerous strategies are utilized in Python comparable to “shutil.copyfile()” methodology, “shutil.copy()” methodology, “learn()” and “write()” features or the “shutil.copyfileobj()” methodology. Most of those approaches make the most of the “shutil” module to repeat the file from the supply/authentic path to the vacation spot path by taking the file with the whole path as an argument. This information introduced numerous strategies to repeat the file in Python using related examples.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments