Open File
#Specify the file path ' filename.txt ', you can use the absolute path and the relative path #mode= ' W ' specifies how files are opened #encoding= ' utf-8 ' Specify file encodingf = open ('filename.txt', mode='W', encoding='Utf-8') F.close ()#Close the file, use the above handle to open the file, the file will always run in memory, after the operation of the file, you should remember to close the file---------------------------------------------------------------------------#when you open a file with the WITH keyword +open, you do not have to close the file with close, and you can open multiple files at the same time, open multiple files with commas separated, and the last handle name with a colonWith open ('filename.txt', mode='W', encoding='Utf-8') as F:#Open a fileWith open ('filename.txt', mode='W', encoding='Utf-8') as F,with open ('filename.txt', mode='W', encoding='Utf-8') as F1:#Open multiple FilesAction file 1. Read
#The original content in the Filename.txt file here is: Life is short, I use Python#①r--Read Only, no error is presentWith open ('filename.txt', mode='R', encoding='Utf-8') as F:content=F.read ()Print(content)>>>life is short, I use Python#②rb--read in bytes typeWith open ('filename.txt', mode='RB') as F:content=F.read ()Print(content)>>> b'\xe4\xba\xba\xe7\x94\x9f\xe8\x8b\xa6\xe7\x9f\xad\xef\xbc\x8c\xe6\x88\x91\xe7\x94\xa8python'#③r+--read/write, does not exist, will not be created, write overwrites previous contentWith open ('filename.txt', mode='r+', encoding='Utf-8') as F:content=F.read () f.write ('\ n Newly added: Life was short, I use Python') Print(content)>>>life is short, I use Python#the content in ④target_file.txt is:>>>life is short, I use Python>>> New additions: Life isShort , I use Python#⑤r+b--Read and write in bytes typeWith open ('filename.txt', mode='r+b') as F:content=F.read () f.write ('\ n Newly added: Life was short, I use Python'. Encode ('Utf-8')) Print(content)>>> b'\xe4\xba\xba\xe7\x94\x9f\xe8\x8b\xa6\xe7\x9f\xad\xef\xbc\x8c\xe6\x88\x91\xe7\x94\xa8python'#the content in Target_file.txt is:>>>life is short, I use Python>>> New additions: Life isShort, I use Python2. Write
#The original content in the Filename.txt file here is empty and needs to be written to: Life is short, I use Python#①w--writes only, does not exist then creates, exists then empties and then writesWith open ('filename.txt', mode='W', encoding='Utf-8') as F:content= F.write ('life is short, I use Python')#the content in Target_file.txt is:>>>life is short, I use Python#②x--is only written, does not exist, is created, there is an errorWith open ('filename.txt', mode='x', encoding='Utf-8') as F:content= F.write ('life is short, I use Python')#Target_file.txt exists:>>>Traceback (most recent):>>> File"d:/python_fullstack_s9/day8/practice.py", line 94,inch<module>>>> with open ('Target_file.txt', mode='x', encoding='Utf-8') as F:>>> fileexistserror: [Errno] File exists:'Target_file.txt'#filename.txt does not exist:>>>life is short, I use Python##③wb--written in bytes typeWith open ('filename.txt', mode='WB') as F:content= F.write ('life is short, I use Python'. Encode ('Utf-8'))>>>life is short, I use Python##④w+--Write, do not exist then create, write will overwrite the previous contentWith open ('filename.txt', mode='w+') as F:content= F.write ('Hello,world') Date=F.read ()Print(date)>>>#the content in Filename.txt is:>>>Hello,world##⑤w+b--read in bytes typeWith open ('filename.txt', mode='w+b') as F:content= F.write ('Hello,world'. Encode ('Utf-8')) Date=F.read ()Print(date)>>> b"'#the content in Filename.txt is:>>> Hello,world3. Append
#The original content in the Filename.txt file here is: Life is short, I use Python, I need to add ' who knows ' content in the back#①a--is appended, does not exist, is created, exists appendWith open ('filename.txt', mode='a', encoding='Utf-8') as F:content= F.write ('Who knows with whom?'the content in Filename.txt is:>>>life is short, I use Python who knows who#②ab--Append as bytes typeWith open ('filename.txt', mode='AB') as F:content= F.write ('Who knows with whom?'. Encode ('Utf-8'))>>>life is short, I use Python who knows who#③a+--readable and writable, non-existent then created, write appendedWith open ('filename.txt', mode='A +', encoding='Utf-8') as F:content= F.write ('Who knows with whom?') F.seek (0) Date=F.read ()Print(date)>>>life is short, I use Python who knows who#④a+b--with bytes type readable and writableWith open ('filename.txt', mode='a+b') as F:content= F.write ('Who knows with whom?'. Encode ('Utf-8')) F.seek (0) Date=F.read ()Print(date)>>> b'\xe4\xba\xba\xe7\x94\x9f\xe8\x8b\xa6\xe7\x9f\xad\xef\xbc\x8c\xe6\x88\x91\xe7\x94\xa8python\xe8\xb0\x81\xe7 \x94\xa8\xe8\xb0\x81\xe7\x9f\xa5\xe9\x81\x93'#the content in Filename.txt is:>>> life is short, I use Python who knows who4. Other operations ①seek () Move cursor pointer position
Seek has three ways to move 0,1,2, of which 1 and 2 must be done in B mode, but regardless of which mode is moved in bytes units
②tell () returns the position where the current pointer is located
Tell is for the English character is one, the Chinese characters accounted for three, the parameter represents the difference between the byte number and read ().
③truncate () Truncate file
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
④readline () reads a row of ⑤readlines () reads multiple lines, returns whether the ⑥readable () file is readable ⑦writeline () writes a row ⑧writelines () writes to a multiline ⑨writable () file is readable ⑩clos is the ED () file closed? encoding= ' Utf-8 ' If the file open mode is B, then there is no attribute? Flush () immediately swipe the contents of the file from the memory to the hard drive? For loop file handle
With open ('filename.txt', mode='r', encoding=' Utf-8') as F: for in f: print(i) >>> Life is short, I use Python>>> who knows with whom
Python Learning day8-file basic operations