Python file processing

Source: Internet
Author: User
Tags throw exception

Tag: NewLine descriptor starts with the body method otherwise a Lin

One, the file operation

When you open a file, you need to specify the file path and how you want to open the file, and then open it to get the file handle and manipulate it later through the file handle.

The mode of opening the file is:

    • R, read-only mode "default mode, file must exist, not present, throw exception"
    • W, write-only mode "unreadable; not exist" created; empty content "
    • X, write-only mode "unreadable; not present, create, present error"
    • A, append mode "readable; not present, create; append content only"

"+" means you can read and write a file at the same time

    • r+, read and write "readable, writable"
    • w+, write "readable, writable"
    • x+, write "readable, writable"
    • A +, write "readable, writable"

"B" means to operate in bytes

    • RB or R+b
    • WB or W+b
    • XB or W+b
    • AB or A+b

Note: When opened in B, the content read is a byte type, and a byte type is required for writing, and encoding cannot be specified

table  File object Methods

method description
f.close () close file, remember When you open a file, you must remember to close it, otherwise it will take up the number of open file handles of the system.
f.fileno () Get file descriptor, is a number
f.flush () refresh output Cache
f.isatty () returns True if the file is an interactive terminal, otherwise false.
f.read ([count]) reads the file and, if there is count, reads count bytes.
f.readline () reads a line of information.
f.readlines () reads all the rows, that is, the information for the entire file is read.
f.seek (Offset[,where]) moves the file pointer to the offset position relative to where. Where 0 indicates the beginning of the file, which is the default value  ;1 represents the current position; 2 means the end of the file.
F.tell () Gets the file pointer position.
f.truncate ([size]) intercepts the file so that the file size is sized.
f.write (string) writes string strings to the file.
f.writelines (list) writes a string of strings to a file in a row, sequential writes to the file, no line breaks.

Read (3) represents reading 3 characters, and the rest of the files within the cursor movement are in bytes, such as: Seek,tell,read,truncate

F.flush () #讲文件内容从内存刷到硬盘 (python3.x)

F.closed #文件如果关闭则返回True

F.encoding #查看使用open打开文件的编码

F.tell () #查看文件处理当前的光标位置

F.seek (3) #从开头开始算, move the cursor to the third byte

F.truncate #从开头开始算, files are kept only from 0-10 bytes of content, and the file must be opened in writing, except W and w+.

Process for file operations

    1. Open the file, get the file handle and assign a value to a variable
    2. Manipulating a file with a handle
    3. Close File

Python file processing

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.