Python Full stack python basics (vii) File operation notes (change add at any time)

Source: Internet
Author: User

File Operation Flow:

1. Open File Opening ()

2. Operation file Read, write
Read (n) n corresponds to the specified number of
Bytes are read in 2.x!
The characters are read in 3.x!
Read reads out the file, starting at the cursor position, and each read one, the cursor moves back one bit.
ReadLine () reads the contents of a row by default. (First line)
ReadLines () reads the contents of each row, forming a list.
Readable () to determine if the file is readable

Change permissions on file mode can be changed

The default is read: r,encoding Specifies the encoding method

Write Operation W encoding specifies the decoding method to write directly to the contents of the original file to overwrite!

When the file is opened, it will clear the previous content, if there is no file, it will first create a file, and then write the content inside! If the file is not closed, the content can always be written in!

Append a appends to the cursor position

X is also a write operation, but the file is not created, some will error, the original file will not be changed

Adjust the position of the cursor F.seek (0) #将光标移到开始位置! Unlike the Read () method, it is moved in bytes.
F.seek (1,0) reads backwards from the beginning of the cursor
F.seek (3,1) reads backwards from the current position
F.seek ( -3,2) read forward from the last position

# This mode must be done in bytes!

Seek application: such as the continuation of the breakpoint.

Show how many bytes F.tell ()

Readable writable mode r+ w+ A +
r+ cursor position, append write

w+ when opening a file, first clear the contents of the previous file, and then write the content,

A + is always added in the last position! The cursor is directly to the last position and reads nothing.

Summarize
R W A w: Overwrite write a: Append write
r+: Default cursor at start position, write must be append write
w+: Overwrite write, want to read content: Seek adjust
A +: The cursor defaults to the last position of the file, regardless of the cursor position, must be an append write; want to read: Seek adjustment

RB WB AB binary operation

RB directly puts the byte data in the disk into memory and does not need to be decoded. WB and AB have the same meaning as RB, and the same thing is to write the data in bytes, without decoding it.


Flush operation

Refresh operation to immediately save the contents of the cached data to disk

Application:

#进度条:

Import Sys
(file, standard output, output content on the screen!) First put the data in the cache, and so on after the end of the operation, and then directly output display! ) sys.stdout.write ("Hello")

The code is as follows:

Import Sys,time



Sys.stdout.write (s)
Sys.stdout.flush ()
Time.sleep (0.5)

' \ R ' is a carriage return, ' \ n ' is a newline, the former makes the cursor to the beginning of the line, the latter makes the cursor move down a grid, usually hit a return, that is, the carriage returns, but also the line (\ r \ n).
For Each loop once, the output results are refreshed again to achieve the function of the progress bar!

With:
With open ("file", mode) as f: #f = open ("file", mode)
F.read ()

File changes
You cannot change the contents of a file after it has been saved.
You can only create a new file, write the content from scratch, delete the original file, and rename the newly written file.

3 closed file Close () data operation will persist until the file is closed, and when the file is closed, all changed data will be saved to disk once.

Python Full stack python basics (vii) File operation notes (change add at any time)

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.