Python growth path 9--file operation Open

Source: Internet
Author: User

One. Open File

f = open ("DB", "R", encoding= "UTF8")

    1. R: Read-only
    2. W: Write only, the file will be emptied first
    3. A: Append
    4. X: If file exists, then error, otherwise, create file and write only
    5. B: The above parameters can be added "B", meaning to open the file in binary form, read writes are bytes, there is no need to consider "encoding"
    6. Encoding= "UTF8": tells the Python interpreter to transcode with UTF8, and when we read the string The interpreter converts the byte into a string, and when we write the string The interpreter automatically converts the string to bytes, which is done by the Python interpreter. We just need to make sure that the encoding is consistent when the file is opened and when the file is saved.
    7. +:r+,w+,a+,x+ are read and write, but we generally use r+, because, w+ will first empty the file, A + is appended, x+ file exists will be error
    8. With open ("FileName", "Open with", encoding= "UTF8") as F: This file is recommended to open without closing the file and can open two files simultaneously, as follows: with open ("db", "R", encoding= "UTF8") as F1, open ("DB2", "W", encoding= "UTF8") as F2

Example: Opening a file with "B"

1 #Open file with b2With open ("DB","WB") as F:3Temp=bytes ("Zhang Yan", encoding="UTF8")4     Print(temp)5 f.write (temp)6With open ("DB","RB") as F:7     Print(F.read ())8 9 Execution Result:TenB'\xe5\xbc\xa0\xe7\x87\x95' OneB'\xe5\xbc\xa0\xe7\x87\x95'
Open file with b

Two. Operating files

    1. Read (): No parameters, read all; parameter (n), file open plus b read n bytes, no b words read n characters
    2. Tell (): Gets the position of the current pointer (in bytes)
    3. Seek (): pointer jumps to the specified position (bytes)
    4. Write (): Writes the data, the file opens plus B, the writing section, and no B words, write characters
    5. Fileno (): File descriptor
    6. Flush (): From memory brush to hard disk
    7. Readable (): File readable return true, otherwise false
    8. ReadLine (): reads a row
    9. Truncate (): Truncate the file, will delete the contents after the pointer
    10. For lines in F: Loop file per line
    11. F.close (): Close file
1 #seek is unaffected by encoding, Seek is byte-by-bit, and if you have Chinese, seek (1) will split this Chinese2With open ("DB","r+", encoding="UTF8") as F:3F.seek (3)4F.write ("Flag")5 6 perform the pre-DB content:7 AB Zhang Yan ijklmnopqrstuvwxyz Zhang Jie8 post-Execution db content:9 ab?flag?ijklmnopqrstuvwxyz Zhang JieTen  One #Tell () Gets the position of the current pointer, as well as seek, in bytes . AWith open ("DB","r+", encoding="UTF8") as F: -     Print(F.read ()) -With open ("DB","r+", encoding="UTF8") as F: the     Print(F.read (2)) -     Print(F.tell ()) -  - Execution Result: + Zhang Yan abcdefghijklmnopqrstuvwxyz - Zhang Yan +6
Seek and tell
1 #F.write () writes from the pointer position, if the pointer overwrites the corresponding byte in the middle of the file2 #However, if you start reading a file, whether or not you read the end of the file, the Python rules will start at the end of the file3With open ("DB","r+", encoding="UTF8") as F:4F.seek (2)5F.write ("Zhang Yan")6 F.seek (0) # actively move the pointer to the beginning of the file7data = F.read (12)8     Print(data)9F.write ("Zhang Jie")Ten f.seek (0) Onedata =F.read () A     Print(data) -  - Execution Result: the ab Zhang Yan Ijklmnop -Ab Zhang Yan ijklmnopqrstuvwxyz Zhang Jie
Write
1 #f.truncate ()2 #truncate the file, it will delete all the contents after the pointer3With open ("DB","r+", encoding="UTF8") as F:4     Print(F.read ())5F.seek (3)6 f.truncate ()7 f.seek (0)8     Print(F.read ())9 Ten Execution Result: One1234567890 A123
truncate

Python growth path 9--file operation Open

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.