Python 3 easy way to read and write files!

Source: Internet
Author: User

Python 3 easy way to read and write files!

A = open (' Test.txt ', ' W ')

This line of code creates a text document named Test, the pattern is write (the pattern is divided into three types, W stands for writing, R is for reading, and a for the tail line is added).

A.write (' hello,world! ')

This line of code is to write data to the Test.txt file, which is written in the ' hello,world! ’。 The data you write is what you see when you open the file.

A.close ()

Remember to close the file when you are finished reading and writing.

Print (A.readline ())

Displays the contents of the first line in the file, executes multiple times, and then displays the second row, the third line ...

A.seek (0)

Go back to the beginning of the file, and if you use the ReadLine method, the position will go to the third row or other location (see which line you are performing), and use Seek () to return to the beginning of the document.

Print (A.read (12))

Displays the contents of the first 12 sizes of a document.

This is the simple Python 3 in the new file, read the file, modify the file method.

Python 3 easy way to read and write files!

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.