Python file read/write

Source: Internet
Author: User

1. Create a file

F=open (Filenamewithpath, ' a ') #创建文件
F.close ()

2. File read

(1) Read all

F=open (file absolute path, ' R ')

Str=f.readlines () #读入全部行的内容, return string

(2) Progressive reading

F=open (file absolute path, ' R ')
line = F.readline () #逐行读取, return string
While line:
line = F.readline ()
F.close ()

3. Progressive Writing

F=open (file absolute path, ' a ') #追加模式
F.write (string + ' \ n ') #逐行写入, add a newline character before or after a string, and do not need to remove ' \ n ' When writing line-wise
F.close ()

4. File Open mode:

W opens in write mode,
A opens in Append mode (starting with EOF and creating a new file if necessary)
R+ Open in read-write mode
w+ Open in read/write mode (see W)
A + opens in read/write mode (see a)
RB opens in binary read mode
WB opens in binary write mode (see W)
AB opens in binary append mode (see a)
Rb+ opens in binary read/write mode (see r+)
Wb+ opens in binary read/write mode (see w+)
Ab+ opens in binary read/write mode (see A +)

Python file read/write

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.