Five. python file processing

Source: Internet
Author: User

1. What is a file
    • Definition: Files commonly referred to in Python are computer files, and computer files are collections of information stored on a computer as a carrier on a computer's hard disk.
    • Features of the file:

1. Readable

2. Writable

3. Can be modified

2. Procedure of File Operation 2.1 Basic operation grammar and rules
    • Syntax: File = open (' File path ', encoding = ' encoded type ', ' mode ')
    • Do not specify open encoding, default to operating system encoding, Windows Gbk,linux to Utf-8, and interpreter encoding Independent

To open the file's mode:

    • 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

2.2 File Read operations
1 #Specify the encoding:2If you do not specify an encoding, the default is read according to the operating system encoding, Windows is gbk,linux to Utf-83 4 #read all at once5F=open ('Test','R', encoding='Utf-8')6data =F.read ()7 Print(data)8F.close ()#The file must be closed after operation. 9 Ten #ReadLine read one line at a time OneF=open ('Test','R', encoding='Utf-8') A Print(F.readline (), end="')#read the first line, end= "to cancel the print wrap - Print(F.readline (), end="')#Read the second line - Print(F.readline (), end="')#read the third line the f.close () -  - #readable determines whether the file is readable, readable returns true, unreadable return false -F=open ('Test','R', encoding='Utf-8') +data =f.readable () - Print(data) +F.close ()#The file must be closed after operation.  A  at #ReadLines read it all and put it in the list. -F=open ('Test','R', encoding='Utf-8') -data =F.readlines () - Print(data)#The data here is a large list -F.close ()#The file must be closed after operation. 
file read Basic operation

2.3 File Write operations

2.4 File Append operation 2.5 file handling other operations

Five. python file processing

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.