1. Simple example of file reading and writing: (Open a file in W-write mode to read a file in R)
#Author:xiajinqi#several ways to read and write files#file read/writef = open ("D://test.txt","W", encoding="Utf-8") F.write ("Hello World") F.flush () F.close () F= Open ("D://test.txt","R", encoding="Utf-8") Data=f.read () data2=F.read () F.seek (0,0) data3=F.read ()Print("Data-------------------", data)Print("data2------------------", Data2)Print("data3-----------------", data3)
2, the file written W and a brief introduction
#Author:xiajinqi#several ways to read and write files#The file is written, the file is written, and if the file exists, the old file contents are emptied (remember) and created if it does not exist. and cannot readf = open ("Test.txt","W", encoding="Utf-8") F.write ("Hello world1\n") F.write ("Hello world2\n") F.write ("Hello world3\n") F.flush () F.close () F= Open ("D://test.txt","R", encoding="Utf-8") Data=F.read ()Print(data) f.close ()#file Append, cannot read, append at end of file, will not empty old filef = open ("Test.txt","a", encoding="Utf-8") F.write ("1 Additional") F.close () F= Open ("Test.txt","a", encoding="Utf-8") F.write ("\ n Append 2") F.close () F= Open ("Test.txt","R", encoding="Utf-8") Data=F.read ()Print(data) f.close () execution result E:\Users\xiajinqi\PycharmProjects\twoday\venv\Scripts\python.exe E:/users/xiajinqi/pycharmprojects/twoday/File.pyhello World1hello world2hello world3hello world1hello world2hello world3 append 1 append 2Process finished with exit code 0
3, the detailed use of the file read R. Several ways and pros and cons of file reading:
1 #Author:xiajinqi2 #Read the file read by the number of lines3 #mode One, read the whole content (the file is too large, memory will explode)4f = open ("Test.txt","R", encoding="Utf-8")5 Print("read it all at once--------------")6 Print(F.read ())7 f.close ()8 9 Ten #Mode 2, one line reads ReadLine, reads all rows, and converts to array f.readlines () One #memory will explode because it has been read into memory, which is called low Af = open ("Test.txt","R", encoding="Utf-8") - Print("One line reads low looper--------------") - forKey,lineinchEnumerate (F.readlines ()): the Print(Key,line.strip ()) - f.close () - - + - #Mode 3: File loop read. Memory is only one line at a time, read a line, close a row, memory will never explode. Recommended use 3, highest efficiency +f = open ("Test.txt","R", encoding="Utf-8") A Print("One line reads bigger looper--------------") at forLineinchF: - Print(Line.strip ()) -F.close ()
4, practice topics, to achieve the Nineth line does not print two ways
Mode one: F = open ("Test.txt", "R", encoding= "Utf-8") print ("Do not print line Nineth") Count = 0for lines in f: count = count + 1< C4/>if count = = 9: print ("Separator >>>>>>>>>>>>>>>>>>>") continue Print (Line.strip ()) F.close () mode two: F = open ("Test.txt", "R", encoding= "Utf-8") print ("No nineth line mode two ") for Key,line in enumerate (F.readlines ()): if key = = 8: print (" Delimiter >>>>>>> >>>>>>>>>>>> ") continue print (Key+1,line.strip ()) F.close ()
5, Seek (), tell () Introduction and summary:
#tell record the position of the current pointer (character position), seek to set the position of the pointer F = open ("Test.txt", "R", encoding= "Utf-8") print ("First print") print (F.read ()) Print (F.read ()) # pointer has reached the end of the file, continue reading will be empty print ("second print") F.seek (0,0) print (F.read ()) f.close () #查找当前位置f = Open (" Test.txt "," R ", encoding=" Utf-8 ") F.readline () print (F.tell ()) F.close ()
6. Summary of other functions of the file:
#f = Open ("Test.txt", "R", encoding= "Utf-8") print ("Other functions use") Print (F.fileno ()) #文件在系统中的编号, general print (F.name) #文件名字print (f.seekable ()) #终端设备无法移动print (F.readable ()) #文件是否可以读f. Close ()
7, flush use. Writes memory data to a file (system default is not real-time refresh)
Import Sys,time # Show progress bar for I in range: sys.stdout.write ("#" ) sys.stdout.flush () time.sleep (0.2)
Python learn to read and write a file (comparison of several ways to read files)