Talking about how to implement two file replication methods in Python, and talking about how to implement two methods in python
The example in this article mainly implements the file copy operation in Python. There are two methods. The specific implementation code is as follows:
# Coding: UTF-8 # Method 1: Simulate file copying using read () and write () # create a file hello.txt src = file ("hello.txt", "w ") li = ["Hello world \ n", "Hello China \ n"] src. writelines (li) src. close () Expose hello.txt to hello2.txt src = file ("hello.txt", "r") dst = file ("hello2.txt", "w") dst. write (src. read () src. close () dst. close () # Method 2: Use the shutil Module # The shutil module is a file and directory management interface that provides functions for copying files and directories # copyfile () the function can copy objects # copyfile (src, dst) # move () function to cut objects # move (src, dst) import shutil. copyfile ("hello. py "," hello2.py ") returns hello.txtcontent to hello2.txt shutil. move ("hello. py ",".. /") Copy hello.txtto the parent directory of the current directory, then delete hello.txt shutil. move ("hell2.txt", "hello3.txt") migrate hello2.txt to the current directory and name it hello3.py. Then delete hello2.txt.
Summary
The above is all about how Python implements two file replication methods. I hope it will be helpful to you. If you are interested, you can continue to refer to other related topics on this site. If you have any shortcomings, please leave a message. Thank you for your support!