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.
I. Basic procedures for file operations
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 ') #默认打开模式就为r #. Manipulate the file through a handle data=f.read () #3. Close File F.close ()
Considerations for closing files
Open a file that contains two parts of the resource: an operating system-level Open file + Application variable. When a file is completed, the two parts of the file must be recovered in a non-landed manner: 1, F.close () #回收操作系统级打开的文件2, Del f #回收应用程序级的变量其中del f must occur after f.close (), Otherwise it will cause the operating system to open the file is 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 completion of the file, we must remember F.close () although I say so, But many students will not forget the face of F.close (), for those who do not long brain, we recommend a fool mode of operation: Use with the keyword to help us manage the context with open (' A.txt ', ' W ') as F:pass with 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
#这就用到了上节课讲的字符编码的知识: To ensure that it is not garbled, how the file stored in what way, it will open in what way. F=open (' A.txt ', ' R ', encoding= ' utf-8 ')
Three. File Open mode
#1. The mode of opening the file has (default is text mode): R, read-only mode "default mode, the file must exist, does not exist" throw exception "W, write-only mode" is not readable, does not exist, it is empty content "A, append write mode only" unreadable, does not exist, then only append the content "#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 mode, Read to the content is a byte type, write also need to provide byte type, can not specify the encoding # #, ' + ' mode (is added a function) r+, read and write "readable, can write" w+, write read "writable, readable" A +, write read "writable, readable" #4, to bytes type of operation of Read and write, read, Write read mode r+b, read and write "readable, can write" w+b, write read "writable, readable" a+b, write read "writable, readable"
Four. File operation method Common file Operation method
Read (3
1. When the file is opened in text mode, the representation reads 3 characters
2. When the file is opened in B mode, the delegate reads 3 bytes
The rest of the files within the cursor movement are in bytes such as: seek,tell,truncate
Attention:
1. Seek has three modes of movement 0,1,2, of which 1 and 2 must be performed in B mode, but regardless of which mode is moved in bytes units
2. Truncate is a truncated file, so the file must be opened in a way that can be written, but not with W or w+, etc., because the file is directly emptied, so truncate to r+ or a or a + and other modes to test the effect.
All action methods.
Class textiowrapper (_textiobase): "" " character and line based layer over a BufferedIOBase object, buffer. encoding gives the name of the encoding that the stream will be decoded or encoded With. it defaults to locale.getpreferredencoding (False) . errors determines the strictness of encoding and decoding (See help (codecs. CODEC) or the documentation for codecs.register) and defaults to "Strict". newline controls how line endings are handled. It can be None, ', ' \ n ', ' \ R ', and ' \ r \ n ' . it works as follows: * On input, if newline is None, universal newlines mode is enabled. lines in the input can end in ' \ n ', ' \ R ', or ' \ r \ n ', and these are translated into ' \ n ' before being returned to the caller. If it is ', universal newline mode is enabled, but line endings are returned to the caller untranslated. if it has any of the other legal values, input lines Are only terminated by the given string, and the line ending is returned to the caller untranslated. * On output, if newline is None, any ' \ n ' Characters written are translated to the system default line separator, os.linesep. if newline is ' or ' \ n ', no translation takes place. if newline is any of the other legal values, any ' \ N ' characters written are translated to the Given string. if line_buffering is true, a call to fLush is implied when a call to write contains a newline character. "" " def close (self, * Args, **kwargs): # real signature unknown Close File pass def fileno (self, *args, **kwargs): # real signature unknown File Descriptor pass def flush (Self, *args, **kwargs): # real signature unknown flush File Internal buffers pass def isatty (Self, *args, **kwargs): # real signature unknown determine if the file is consent to the TTY device pass def read (Self, *args, **kwargs): # real signature unknown read the specified byte data pass def readable (Self, *args, **kwargs): # real signature unknown is readable pass def readline (Self, *args, **kwargs): # real signature unknown Read only one row of data pass def seek (Self, *args, **kwargs): # real signature unknown pointer position in specified file pass &nBsp;def seekable (Self, *args, **kwargs): # real signature unknown whether the pointer is operable pass def tell (Self, *args, **kwargs): # real signature unknown Get pointer position pass def truncate (Self, *args, **kwargs): # real signature unknown truncate data, preserving only the data that you specified before pass def writable (Self, *args, **kwargs): # Whether real signature unknown can write pass def write (Self, *args, **kwargs): # real signature unknown Write Content pass def __getstate__ (Self, *args, **kwargs): # real signature unknown pass def __ init__ (Self, *args, **kwargs): # real signature unknown pass @staticmethod # known case of __ new__ def __new__ (*args, **kwargs): # real signature unknown "" " create and return a new object. see help (type) for accurate signature. "" " pass def __next__ (Self, *args, **kwargs): # real signature unknown "" " implement next (self) . " ""         PASS    DEF __REPR__ (Self, *args , **kwargs): # real signature unknown "" " return repr (self) . " "" pass buffer = property (Lambda self: object (), lambda self, v: None, lambda self: none) # default closed = Property (Lambda self: object (), lambda self, v: none, lambda self: none) # default encoding = property (lambda self: object (), lambda self, v: none, lambda self: none) # Default &nbsP;errors = property (Lambda self: object (), lambda self, v: none, Lambda self: none) # default line_buffering = Property (Lambda self: object (), lambda self, v: none, lambda self: none) # default name = property (lambda self: Object (), lambda self, v: none, lambda self: none) # Default newlines = property (Lambda self: object (), lambda self, v: none, lambda self: none) # default _chunk_size = property (Lambda self: object (), lambda self, v: None, lambda self: none) # default _finalizing = property (lambda self: object (), lambda self, v: none, lambda self: none) # default3.x
Five. Modification of documents
File data is stored on the hard disk, so there is only coverage, there is no modification so to speak, we usually see the modified files, are simulated out of the effect, specifically, there are two ways to achieve:
Method One:
Mode one: The contents of the file stored in the hard disk are loaded into memory, can be modified in memory, and then overwritten by memory to the hard disk (word,vim,nodpad++ and other editors).
Import OS # calls the System module with open (' a.txt ') as Read_f,open ('. A.txt.swap ', ' W ') as Write_f:data=read_f.read () #全部读入内存, if the file is large, it will Very data=data.replace (' Alex ', ' SB ') #在内存中完成修改 write_f.write #一次性写入新文件os. Remove (' a.txt ') #删除原文件os. Rename ('. a.txt . Swap ', ' a.txt ') #将新建的文件重命名为原文件方法一
Method Two:
Mode two: The contents of the file stored on the hard disk are read into memory line by line, the modification is completed to write a new file, and finally overwrite the source file with a new file
Import Oswith Open (' a.txt ') as Read_f,open ('. A.txt.swap ', ' W ') as Write_f:for line in Read_f:line=line.replace ( ' Alex ', ' SB ') write_f.write (line) os.remove (' A.txt ') os.rename ('. A.txt.swap ', ' a.txt ')
File operations in Python