Python learning -13.python input and output (ii)

Source: Internet
Author: User

In Python, read the file using the Open function

1 file=open (r'E:\temp\test.txt','r') 2 var = file.read ()3print(Var)4 file.close ()

The first line opens the E:\temp\test.txt file, noting that the beginning of the string uses R,r to indicate that the content inside the string is not escaped, like @ in C #. In Python, if you want to make the string non-escaped, add R or R at the beginning of the string. The second parameter indicates a read-only file.

' R ' Read-only
' W ' Write only
A Additional
' B ' Binary
' r+ ', ' A + ' Update

The second line reads the contents of the file and assigns the value to the VAR variable.

The third line prints the value of the Var variable.

Line four closes the file.

Read method
Read () Read all, return a string
ReadLine () Reads only one row at a time, returns a string
ReadLines () Read all, return to list

Python learning -13.python input and output (ii)

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.