Day eighth read, write, append, read/write, read, Seek () move the cursor, modify the file, and another way to open the file

Source: Internet
Author: User

  

Main content:
1. Initial file operation

2. Read-only (R,RB)

3. Write only (W,WB)

4. Append (A, AB)

5. Read and write (r+)

6. Write-read (w+)

7. Additional write read (A +)

8. Other methods of operation

9. File modification and another way to open a file handle

A. Operation of the initial file

using Python to read and write files is a very simple operation. We use the Open () function to get to a file handle by opening the file. Then you can do all sorts of things with a file handle. There are also differences in the actions you can perform depending on how you open them.

How to open the file: R, W, A, r+, w+, A +, RB, WB, AB, R+b, w+b, a+b the default is R (read-only) mode

two. read-only Operation

  

Attention:

      1. Encoding represents the encoding set. Data is obtained based on the actual saved code of the file, for us. More is the utf-8.

2. RB. The data read is bytes type, in RB mode. Cannot select encoding Character set

   RB's role:

When reading a non-text file. such as reading MP3. Image. You need to use RB when it comes to video and other information. Because there is no way to display this data directly. We will use it later when we upload and download the file. And we watched the live broadcast. It's actually all this data.

  There are two types of paths:

      1. relative path: relative to the folder where your current program is located (must be mastered) ( .. /) Return to the previous level directory
      2. Path by alignment:
        1. Looking from the root directory of the disk
        2. A pair of paths on the Internet

We recommend that you use a relative path. Because when we copy the program to someone else's use. It can be run directly by copying the mesh. But if you use absolute paths. It is also necessary to copy the external files.

ways to read files: 1. Read () reads all the contents of the file,  

Cons: Account for memory. If the file is too large. Easy to cause memory crashes

    

 2. Read (n) reads n characters,

    It is important to note that:

If it is read again, it will continue reading at the current position instead of reading from the beginning, and if you are using RB mode, it will read n bytes.

    

3. ReadLine () reads one row of data at a time

Attention:

At the end of the ReadLine (), notice that every time the data is read, there's a \ n so. Need us to make the Strip () method to remove \ n or space

    

4. ReadLines ()

Each line is formed into an element and placed in a list. All the content is read out. So it's also prone to memory crashes. Deprecated

    

5. Loop-read.

This is the best way to do it. Read one content at a time. No memory overflow issue

    

Note: After reading the file handle, be sure to close f.close

three. Write mode

Note: (Pit )

When writing, if there is no file, the file is created. If the file exists, the original content is deleted and then written (that is, the original content is replaced)

    

Four. Append (A, AB)

in Append mode, the content we write will be appended to the end of the file.

  

Five. Read/write mode (R+,R+B)

For read-write mode. Must be read first. Because the default cursor is at the beginning. Ready to read. When you are finished reading, write again. The model we use most often is r+.

r+ mode, by default the cursor at the beginning of the file, must read first, write

     Remember: r+ mode. Must be read first. And then write

    

     No matter how many you read in the file before, write it back, and add it at the end of the file.

   1. Write before the document is done, and write it from the beginning

2. If you read something in a document, write it at the end of the document

    

Six. Write Read (w+,w+b)

    Clear all the contents first. and then write. Last read. But the read content is empty, not commonly used

  

Seven. Seek (n) cursor moves to n position

    Attention:

The unit to move is byte. So if the Chinese part of UTF-8 is a multiple of 3.

Usually the use of seek () is moved to the beginning or end:

Move to the beginning: Seek (0)

Move to the end: Seek (0,2)

The second parameter of seek indicates where to go in the line offset, the default is 0, which means the beginning, 1 means the current position, and 2 means the end

   Use of seek in read-write mode (r+)

    

Deep Pit Please note:

In r+ mode. If the content is read. No matter how much content is read. How much the cursor is showing. When you write to or manipulate the file, it is done at the end.

Tell ()

    Use Tell () to help us where the current cursor is located

Print (F.tell ()) Gets the cursor

truncate () truncate file
    1. Truncate () does not give parameters, starting from the file header, intercept to the current position (that is, the position indicated by the cursor)
    2. Truncate (parameter) gives the parameter, starting with the file header, to intercept the position of the given parameter

Eight. How to modify a file and how to open it in another way

File modification:

You can only read the contents of the file into memory, modify the information, and then delete the source file, and change the name of the new file to the name of the old file.

Remove () Delete file

Rename () Renaming files

To modify a file:

Import OS

with open (' filename ', mode= ' Read method R ', encoding= ' Utf-8 ') as f1,\

Open (' New filename ', mode= ' Read method w ', encoding = ' utf-8 ') as F2:

    

Cons: All content is read at once. Memory overflow. Solution: Read and operate one line at a

Nine. Determine what the file can do

Readable to determine if a file is readable

Writable to determine if a file is writable

    

Day eighth read, write, append, read/write, read, Seek () move the cursor, modify the file, and another way to open the file

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.