#__author: "Feng Lin"#Date:2018/8/27#pycharm using UTF-8 encoding format by default#file Operations#Head teacher. txt#1. File path: d:\. txt#2. Encoding method: UTF-8,GBK ...#3. Operation mode: Read-only, write-only, append, read/write, read#Note: Files stored in what encoding mode will be opened for Operation#Read-only#F=open (' head teacher ', mode= ' R ', encoding= ' utf-8 ')#Context=f.read ()#Print (context)#f.close ()#Open Non-text file, the sample is open with RB mode, bytes type open#F=open (' head teacher ', mode= ' RB ')#Context=f.read ()#Print (context)#f.close ()#Write only#for Mode=w, no files are created#f=open (' Log ', mode= "w", encoding= "Utf-8")#f.write ("Hello, world! ")#f.close ()##mode =w, if the file exists, it clears the contents of the source file, overriding#f=open (' Log ', mode= "w", encoding= "Utf-8")#f.write ("Hello, world!") ")#f.close ()##f=open (' Log ', mode= ' WB ')#f.write (' Hello, world! '. Encode (' UTF '))#f.close ()#F=open (' head teacher ', mode= ' a ', encoding= ' utf-8 ')#f.write (' hehe ')#f.close ()##以bytes类型追加#F=open (' head teacher ', mode= ' ab ')#f.write (' hehe '. Encode (' Utf-8 '))#f.close ()#read/write mode mode=r+ Note cursor position#F=open (' head teacher ', mode= ' r+ ', encoding= ' utf-8 ')#print (F.read ())#F.write (' Dream, xx ')#print (F.read ())#f.close ()#read-write mode mode=r+b open read/write with bytes type#F=open (' head teacher ', mode= ' r+b ')#print (F.read ())#f.write (' Dharma, qingming ' encode (' Utf-8 '))#f.close ()#read-write mode, mode=w+#f=open (' Logs ', mode= ' w+ ', encoding= ' utf-8 ')#f.write ("Big Frog in the event")#f.seek (0)#print (F.read ())#f.close ()#additional readable mode, mode=a+#f=open (' Logs ', mode= ' A + ', encoding= ' utf-8 ')#f.write ("slightly")#f.seek (0)#print (F.read ())#f.close ()#functionF=open ('logs', mode='r+', encoding='Utf-8')#Content=f.read (3) reads the minimum characters .F.seek (3)#is the position of the cursor according to the byte#F.tell () tells you where the cursor is.Print(F.tell ())#f.readable () returns the bool value to determine if it is readableContent=F.read ()Print(content)#ReadLine () one line readLine=F.readline ()Print(line)#ReadLines () treats each row as an element in the list and adds it to the listf.seek (0) lines=F.readlines ()Print(lines)#can act as a list forIinchlines:Print(i)#With open can not write close ()With open ('Log', mode='r+', encoding='Utf-8') as F1, open ('Log', mode='r+', encoding='Utf-8') as F2:Print(F1.read (), F2.readlines ())
Python Basic Learning-file manipulation