Talking about how to implement two file replication methods in Python, and talking about how to implement two methods in python

Source: Internet
Author: User

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!

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.