11_python file Operations

Source: Internet
Author: User
Tags readline

First, the basic process of file operation

The computer system is divided into three parts: the hardware, the operating system and the application.

Applications that we write in Python or other languages need to be saved on the hard drive if we want to keep the data permanently, which involves the application operating hardware, and it is well known that the application is not able to manipulate the hardware directly. The operating system encapsulates complex hardware operations into a simple interface for use by the user/application, where the file is the operating system provided to the application to manipulate the virtual concept of the hard disk, and the user or application can save its own data permanently by manipulating the file.

With the concept of a file, we do not have to consider the details of the operation of the hard disk, only to focus on the process of manipulating files.

To manipulate a file first think about what parameters are required:

    1. The path where the file is located, such as: D:\test.txt
    2. The encoding used to read the file
    3. Operation mode: Read, write, append ...
    4. Close files, release resources

Read D:\test.txt file, save format utf-8:

# in Python, we get a file handle through open (), and then we use the file handle to implement the operation of the file f = open('d:\\test.txt', Encoding= 'utf-8', mode='r'#  Read file contents f.close ()print(content)
Ii.. File encoding

F=open (...) is opened by the operating system file, then if we do not specify the encoding for open, then the default encoding of the opening file is obviously the operating system, the operating system will use its own default encoding to open the file, under Windows is GBK, under Linux is Utf-8.

# This is the use of the last lesson of the character encoding knowledge: to ensure that no garbled, the file in what way, it will be opened in what way. F=open ('a.txt','r', encoding='utf-8  ')
Third, File open mode

File handle = open (file path, operation mode, encoding)

" " 1. Read for text file: R: Read-only mode W: write-only mode A: Append mode 2. For non-text file reads: RB: In binary read-only mode WB: Binary write-only mode ab: binary append mode 3.+ is the function enhancement r+: Open in read/write mode w+ : Open a + in read-write mode: Open in read-write mode 4. Mode with bytes Type Rb+/r+b open in binary read-write mode wb+/w+b open in binary read-write mode ab+/a+b open in binary read-write mode 
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
Iv. file operation Methods 4.1 common methods of operation
1 #1.read () overall read out2f = open ('D:\\test.txt', encoding='Utf-8')#R can be omitted3Content =F.read ()4 Print(content)5 6 #2.readline () read one line7f = open ('D:\\test.txt', encoding='Utf-8')8line1 = F.readline ()#Read only one line at a time9Line2 =F.readline ()Ten Print(line1) One Print(line2) A  -  - #3.readlines () read, each row as a list element, return a list theLi = [] -f = open ('D:\\test.txt', encoding='Utf-8') -Li =F.readlines () - Print(LI)#[' This is a file.\n ', ' Python3 file Operate method.\n '] +  - #4.readable () +f = open ('D:\\test.txt', encoding='Utf-8') AL = f.readable ()#whether it is readable at Print(L) -  - #5. Read large files, if a file is 16G, far more than the size of our ordinary computer memory, how to read -f = open ('D:\\test.txt', encoding='Utf-8') -  forIinchF:#file handle, one line of Read -     Print(i) in  - #6. Read n characters, in RB mode, read by byte tof = open ('D:\\test.txt', encoding='Utf-8', mode='R') +Content = F.read (3) - Print(content)#Thi Chinese People the  * #RB mode without encoding= ' UTF-8 ' $f = open ('D:\\test.txt', mode='RB')Panax NotoginsengContent = F.read (3) - Print(content)#b ' \xe4\xb8\xad ' the Print(b'\xe4\xb8\xad'. Decode ('Utf-8'))#in
4.2 Write-only operations
1f = open ('D:\\test.txt', mode='W', encoding='Utf-8')2 ifF.writable ():#determine if the file is writable3F.write ('New Content')#If the file exists, overwrite the original content, if the file does not exist, create the file, write the content4 ifF.readable ():#cannot be read, so no print5     Print(F.read ())
4.3 Append operation
1f = open ('D:\\test.txt', mode='a', encoding='Utf-8')2 ifF.writable ():#determine if it can be written3F.write ("what's added")#If the file exists, append to the original content, and if the file does not exist, create the file, write the content4 ifF.readable ():#to determine whether it is readable or unreadable5F.read (F.read ())
V. Movement of the cursor

The Seek (), tell (), truncate () cursor movements are in bytes .

1. Seek has three ways of moving 0,1,2, of which 1 and 2 must be performed in B mode, but regardless of the mode, it is 2 moving in bytes units  . Truncate is a truncated file, so the file must be opened in a writable manner , but can not be opened with W or w+, because that will directly empty the file , so truncate to r+ or a or a + and other modes to test the effect.
Seek ()

The Seek () method is used to move the pointer (the file reading pointer) to the specified position.

Tell () truncate ()

  

11_python file Operations

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.