Introduction to Python file and stream-related knowledge

Source: Internet
Author: User
Here is a small series for you to bring a Python file and stream (the example explained). Small series feel very good, now share to everyone, also for everyone to make a reference. Let's take a look at it with a little knitting.

1. File Write


#打开文件, the path error will be incorrect F = open (R "C:\Users\jm\Desktop\pyfile.txt", "W") F.write ("hello,world!\n") F.close ()

2. File read


#读取f = open (R "C:\Users\jm\Desktop\pyfile.txt", "R") print (F.read ()) f.close () Output: hello,world!

3. Read and Write lines

ReadLine ([size]) reads the entire line, including the "\ n" character.

ReadLines ([sizehint]) reads all rows and returns a list, and if given sizeint>0, returns a row with a sum of approximately sizeint bytes, the actual read value may be larger than sizhint because the buffer needs to be populated.

Writelines (Sequence) writes a list of sequence strings to the file and adds a newline character to each line if a line break is required.

4. File objects are iterative and can be used directly in a for loop.

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.