Python file processing

Source: Internet
Author: User

#when writing and reading, all data can only be a string, not a number or other content#create a new file; Write the contents of the file as: This is a new file contentf = open ('a.txt',"W", encoding="UTF8") F.write ("This is a new file content \ n")#write operation \ n carriage return to newlineF.write ("This is a new file content \ n")#Write OperationsF.writelines (['111\n','222\n'])#list way to write multiple linesPrint(F.writable ())#determine if it can be writtenf.close ()#Open the file, read the contents of the file, do not write ' R ', the default is read-only modeR = Open ("a.txt",'R', encoding="UTF8") Data= R.read ()#Read all contentPrint(data)Print(R.readable ())#determine if a file is read-onlyPrint(R.readline ())#read one line at a time, the next line of content can be run ReadLine, the default last line has a carriage return, there will be a newline casePrint(R.readline (), end="")#remove the last carriage returnPrint(R.readlines ())#Read all the content, put the contents of each row into a list, the default last line has a spacer.close ()#Appendf = open ('a.txt',"a", encoding="UTF8") F.write ("This is the appended content \ n") F.close ()#r+ can read and write#f = open (' A.txt ', "r+", encoding= "UTF8")#F.write ("This is the content of r+ \ n") #默认第一行开始写入 to overwrite the existing content directly in the order of modification#f.close ()#Open without closing the file with the can, automatically close the fileWith open ('a.txt','R', encoding="UTF8") as F:data=F.read ()Print(data)#str----> Encoding (encoded)----->bytes#Btyes---> Encode (decode)------->str#Open Mode" "do not fill in the default ' r ' Open for Reading (default) ' W ' open for writing, truncating the file first '  X ' Create a new file and open it for writing ' a ' open for writing, appending to the end of the file if It exists ' B ' binary mode does not fill in the default ' t ' text mode ' + ' open a disk file for upd Ating (Reading and writing) ' U ' universal newline mode (deprecated)" "#b mode is the binary way:#Writef = open ('Binary Files','WB')#WB Binary Mode write file, binary file is not encoded, only decoding#f.write (' 1111, hello ') #报错 typeerror:a Bytes-like object is required, the not ' str ' binary file is written, need to be in binary mode, cannot directly write Str into the inside #Processing method: You can first convert the string into binary mode in the write#string to binary process (encoding):Test = bytes ('1111, hello.', encoding="Utf-8")#bytes and ' 1111, hello '. encode= "Utf-8 code is the same; one is the bytes method one is the string itself provides a methodf.write (test)#Readf = open ('Binary Files','RB')#Binary way to read filesdata =F.read ()Print(data)#B ' 1111,\xe4\xbd\xa0\xe5\xa5\xbd ' reads a binary file, so it needs to be decoded into a string.#Binary to string process (decode):Print(Data.decode ('Utf-8'))#after decoding: 1111, hello" "One: Read (3): When the file is opened in text mode, the delegate reads 3 characters; When the file is opened in B mode, the cursor movement within the rest of the file is read by 3 bytes as Seek,tell,truncate Note: 1. Seek There are three ways to move 0,1,2, of which 1 and 2 must be done in B mode, but regardless of the mode, it is 2 moving in bytes. Truncate is a truncated file, so the file must be opened in a way that can be written, but cannot 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" "F= Open ('Binary Files','r+', encoding="Utf-8")" "The binary file contains the following: Hello" "F.readline ()Print(F.tell ())#Tell the current position of the cursor is equal to 8 why is 8 because "hello" a Chinese is 3 bytes plus win platform default carriage return is "\ r \ n" So 8F.seek (1)#seek cursor jumps to the 1th byte positionPrint(F.tell ()) F.truncate (3)#from the start count, keep the file security only from 0-3 bytes of content#Seek Advanced ModeF.seek (10,0)#0 is the default, not write is the default 0; From the beginning of the file 10 locationsF.seek (10,1)#1 relative position, is the last position, in the number of 10 positions, this requires B mode to open the file, or errorF.seek ( -10,2)#2 reverse, is read backwards, in the number of 10 positions, this requires B mode to open the file, or errorf.close ()#Displays the default carriage return for the win platform; NewLine displays line breaks in the most primitive wayf = open ('Binary Files','r+', encoding="Utf-8", newline="') Data=F.readlines ()Print(data)#[' Hello \ r \ n ', ' 1111 '] #newline效果就显示 \ r \ n; No newline by default it is [' Hello \ n ', ' 1111 '] less a \ r because Python is optimized for you.

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.