One, 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 no longer have to consider the details of the operation of the hard disk, just the process of manipulating the files:
# 1. Open the file, get the file handle and assign a value to a variable f=open ('a.txt','r', encoding='utf-8'# The default open mode is R#2. Manipulating files with a handle data=f.read ()#3. Close file f.close ()
Open a file containing two parts of the resource: the operating system-level Open file +the variable for the application. When you have completed a file, you must recycle the two parts of the file with a non-landed method:1, F.close ()#Recycle open files at the operating system level2.delF#Recycle application-level variableswhere del F must occur after f.close (), otherwise it will cause the operating system open files are not closed, the use of resources, and Python automatic garbage collection mechanism determines that we do not have to consider Del F, which requires us, after the operation of the file, Be sure to remember F.close () although I say so, but a lot of people will forget F.close (), here we recommend the stupid way: use with keyword to help us manage the context with open ('a.txt','W') as F:PassWith Open ('a.txt','R') as Read_f,open ('B.txt','W') as Write_f:data=Read_f.read () write_f.write (data) Note
Two, 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.
Three, open mode of the file
# 1. The mode of opening the file has (default is Text mode:) R, read-only mode "default mode, file must exist, does not exist" throws exception "W, write-only mode" is unreadable, no existing is created; "A, append write mode only" is not readable, does not exist, is created;
#1. Read it all out f.read () F = open (' Log.txt ', encoding= ' utf-8 ', mode= ' r ') content = F.read () print (content) F.close () #2. A line of Read F = open (' Log.txt ', encoding= ' Utf-8 ') print (F.readline ()) print (F.readline ()) print (F.readline ()) F.close () #3: Each line of the original file as a list of elements F = open (' Log.txt ', encoding= ' Utf-8 ') print (F.readlines ()) F.close () #4: Reads part read (n) #在r模式下, read (n) Read according to the characters. f = open (' Log.txt ', encoding= ' Utf-8 ') print (F.read (3)) F.close ()
# 2. For non-text files, we can only use B mode, "B" means to operate in bytes (and all files are stored in bytes, using this mode regardless of the text file character encoding, picture file JGP format, video file avi format)RB Wbab Note: When opened in B, the content read is byte type, write also need to provide byte type, cannot specify encoding #3, ' + ' mode (is added a function)r+, read and write "readable, writable" W+ , write read"writable, readable" A +, write read "writable, readable"#4, read/ write, read/write to bytes type, r+ B, read and write"readable, writable" W+ B, write read"writable, readable" a +b, write "writable, readable"
Python Full stack learning--day8