Python file Operations

Source: Internet
Author: User

1. File reading and writing process

1. Open File

2. Write the contents to the file;

3. Close the file

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M00/9B/EE/wKiom1lozQvxE-OkAAAZ8UD4Btg732.png "title=" Xnywiyak5v1 (5{}be]i%@%7.png "alt=" Wkiom1lozqvxe-okaaaz8ud4btg732.png "/>

2. file reading and writing

If the file is small, read () One-time reading is most convenient;

If the file size cannot be determined, the read (size) is called repeatedly

Compare insurance; If it is a configuration file, call ReadLines ()

Binary file, open with ' RB ' mode

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M01/9B/ED/wKioL1lozWXwanXWAAAbhFv6s60485.png "title=") ~ knbot_qz]b[k@ (25zh27.png "alt=" Wkiol1lozwxwanxwaaabhfv6s60485.png "/>

Pattern of 3.open functions

R opens in read mode, navigates to the beginning of the file, the default mode

R+ Open as read-write, locate the beginning of the file, can write content to the file

W is opened in writing, the contents of the file are emptied when the file is opened, and cannot be read

w+ opens in read-write mode, navigates to the file header, and clears the contents of the file when the file is opened

A writes open, navigates to the end of the file, is an append operation, but does not allow read

A + opens in a read-write manner, navigates to the end of the file, and appends the method

650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M00/9B/EE/wKiom1lozjywt411AABpNx0otYA449.png "style=" float : none; "title=" CQ35%T68RMNH (9[ju_21c78.png "alt=" Wkiom1lozjywt411aabpnx0otya449.png "/>

650) this.width=650; "src=" Https://s5.51cto.com/wyfs02/M00/9B/ED/wKioL1lozj2zrv6lAAAVthRL4tg657.png "style=" float : none; "title=" M1supc7q8h11[[zj51$tcn3.png "alt=" Wkiol1lozj2zrv6laaavthrl4tg657.png "/>

4. Other operations of the file


F.flush () function to write the contents of the buffer to the hard disk

F.seek (offset[,whence]), offset indicates how many bytes are moved, whence is 1 when it moves relative to the current position, and when 2 moves backwards from the end of the file, but not all platforms are supported; The default is 0 to move backward from the beginning of the file

650) this.width=650; "src=" Https://s4.51cto.com/wyfs02/M00/9B/ED/wKioL1loz4CBkOA6AABTuA5Y1G0479.png "title=" M ' ~n4 [%AH1] Mk~~r[k~[6dm.png "alt=" Wkiol1loz4cbkoa6aabtua5y1g0479.png "/>

F.tell () function, which returns the offset of the current file pointer

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M01/9B/ED/wKioL1lozsihI7e_AAAaCdsByjw728.png "style=" float : none; "title=" 3h3lx (N5$}%NKXR) 22CS (v1.png "alt=" Wkiol1lozsihi7e_aaaacdsbyjw728.png "/>

650) this.width=650; "src=" Https://s3.51cto.com/wyfs02/M02/9B/EE/wKiom1lozsihCq4KAAAT8cCYYwQ242.png "style=" float : none; "title=" N) 79A (h{9qxo6ny{[mq6o2k.png "alt=" Wkiom1lozsihcq4kaaat8ccyywq242.png "/>


5.with syntax

In general, open a file, and after it has been manipulated, explicitly execute xx.close () to close the file. With for operations that need to be turned on and off, the open object can be closed automatically.

650) this.width=650; "src=" Https://s1.51cto.com/wyfs02/M00/9B/EE/wKiom1lozzuSFCOaAAAy0dB2OGM122.png "title=" Pmmu} jebc}]xfan[~) qw%qj.png "alt=" Wkiom1lozzusfcoaaaay0db2ogm122.png "/>


Python file Operations

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.