Python's operations on files

Source: Internet
Author: User
Tags readable

One, open mode of the file
Open mode Description
R Read-only mode open, default
W Write-only opening, written in append form, if file is directly overwritten, if there is no file created
A Write-only open, if the file exists append content to the end, if no file exists
+ All modes can be added, indicating a readable and writable
Ab Binary write-only mode open, if the file exists append to the end, if no file exists
Rb Binary mode Open
Wb Binary write, if the file exists directly overwrite, if there is no file created

Second, the operation of the file

1,f.close () Close file

2,f.write (b) Write the content, note that if the W mode means writing with a string, WB represents a binary write.

3,f.closed returns True if the file is closed, false otherwise.

4,f.fileno () returns the file descriptor.

5,f.flush () Refreshes the internal buffer of the file, directly writes the data of the internal buffer to the file immediately, instead of passively waiting for the output buffer to be written.

6,f.encoding the encoded format of the returned file

7,f.readline ([size]) if size does not read the data one line at a time, if there is a size, the size character is read out

8,f.readlines () reads all the files, returns the list

9,f.tell () returns the position of the file pointer

10,f. seek(offset[, whence]) sets the current bit position of the file pointer, Offset --The starting shift, which is the number of bytes required to move the offset,whence: optional, the default value is 0. Give the offset parameter a definition of where to start the offset, and 0 to start at the beginning of the file, 1 to start at the current position, and 2 for the end of the file.

11,f.name Return file name

f.seekable () file cursor is movable

13,f.readable () file is readable

14,truncate

14,f.writeable () file is writable

15,f.read ([size]) reads a file of size bytes, such as size without or negative, reading the entire content.

16,f.truncate (size) intercepts a size character, starting at the cursor by default.

17,f.writelines (Sequence) writes a file, which is an iterative object that can return a string or binary

Third, read the file
    1.  for  in F.readlines (): #返回成列表后读取

    2.  for  in F:  #f为迭代器, when the file is larger, this is better

Python's operations on files

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.