python--i/o

Source: Internet
Author: User
Tags throw exception

CPU Composition: Computing: The completion of various arithmetic operations, logic operations, data transmission and other processing processing controller: the implementation of the program memory: for memory programs and data, such as memory input device: The data or program input into the computer, for example: Mouse output device: Data or program processing results presented to the user For example: Display, printer and other general I/O operations, refers to the file IO, if the network IO will be directly referred to as network IO file IO Common operations: Open: Opened read: Read write: Write close: ReadLine: Line Read ReadLines: Multi-line read seek: File pointer operation tell: pointer position open (file, mode= ' r ', buffering =-1, encoding= none, errors = none, newline = none, C LOSEFD = Ture, opener = None) opens a file that returns a file (stream) object and a file descriptor. Failure to open the file returns an exception f = open (' Test ') #file对象 opens the test file F.read () read file F.close () the access mode of the close file file is two: The text mode and the binary mode different modes, the operation function is not the same, The results are different. Open parameter: File: Opens or the filename to be created, if no path is specified, the default current working path (relative path) mode-> mode: #r mode--readable not writable F = open (' Test ') f.read () f.write  (' abc ') #不可写f. Close () F = open (' Test ', ' R ') #使用r read-only method not writable F.write (' abc ') f.close () #打开一个不存在的文件 Error F = open (' test1 ', ' r ') #w mode f = Open (' Test ', ' W ') f.write (' CCC ') F.close () >>>cat test>>> ccc# in use once write function directly overwrite test to null f = open (' Test ', mode = ' W ') F.close () >>> cat test>>> #w can directly create a new test1 write ' 123 ' F = open (' test1 ', mode= ' W ') f.write (' 123 ') F.close () >>>cat test1>≫>123%open default is read-only mode R to open files that already exist. R: Read-only open file, if using the Write method, throw exception if the file does not exist, throw Filenotfounderror exception W: write-only open, read and throw exception file does not exist, you can create the file directly if the file exists, Empty file contents-----------------------------------------------x: File does not exist, create file, only writable file exists, throw Fileexistserror exception f = open (' Test2 ', ' X ') F.read () f.write (' ABCD ') f.close () >>>cat test2>>>abcdf = open (' Test2 ', ' X ') A: file exists, writable can be opened, Append content file does not exist, after creation, writable can be opened, append content f = open (' Test3 ', ' a ') F.read () f.write (' zzzzzzz ') f.close () >>cat test3>>> ZZZZZZZF = open (' test4 ', ' a ') f.wirte (' test4 ') f.close () >>>cat test4>>>test4 Summary: R is read-only, Wxa are all write-only wxa can produce new files. W whether the file exists or not, will generate a completely new content file a regardless of whether the file exists, can be appended to the end of the open file X must require that the file does not exist in advance, create a new file text mode T: Character stream, the file's bytes according to a character encoding understanding, according to character manipulation. The open default mode is RT binary mode B: Byte stream, which understands the file in bytes, regardless of character encoding. In binary mode operation, the byte operation uses the bytes type F = open (' Test ', ' WB ') f.write (' ABC test '). Encode ()) f.close () F = open (' Test ') F.read (3) >>> BC Test F.read (1) >>>af.close () F = open (' Test ', ' RB ') F.read (1) >>> ' A ' f.read (3) >>> ' Bc\xe6 ' F.close ()--------------------------------------------------F = open (' Test ', ' r+ ') f.write (' abc123 ') F.read () F.close () F = open (' Test ', ' w+ ') f.write (' abc123 ') F.read () F.close () F = Open (' Test ', ' A + ') f.write (' abc123 ') F.read () F.close () F = open (' Test2 ', ' x+ ') f.write (' abc123 ') F.read () F.close () The above f.read are empty, but cat can display the content +: to provide r/w/a/x with missing read and write functions (lack of reading to read, missing write), however, get the file object is still in accordance with r/w/a/x own characteristics. + cannot be used alone, you can think of it as an enhancement for the preceding pattern character. File pointer: Point to current byte position mode = R pointer starts at 0mode = A pointer starts at EOF (end of file) tell () show pointer current position seek (Offset[,whence]) move file pointer position. Offset offset x Byte text mode: whence 0 default, offset can only be positive integer whence 1 indicates starting at current position, offset only accepts 0whence 2 means starting with EOF, offset only accepts 0test4 = (' Te St4 ') F = open (' test4 ', ' r+ ') F.tell () #起始 0f.read () # ' test4 ' F.tell () # EOF 5f.seek (0) # 0f.read () # Test4f.seek (2,0) # 2f. Read () #st4f. Seek (2,0) #st4f. Seek (2,1) #Error offset can only be 0f.seek (2,2) #Error offset can only be 0f.close () Chinese f = open (' test4 ', ' w+ ') F.write (' Test ') F.tell () #6f. Close () F = open (' test4 ', ' r+ ') F.read (3) # ' Test ' F.seek (1) #1f. Tell () #1f. Read () #Errorf. Seek (2) #2f. Close () text mode supports backward offset from the beginning in binary mode: whence 0 default value, which means that offset can only be a positive integer whence 1 indicates starting from the current position, offset can be positiveNegative whence 2 means starting with EOF, offset can be positive or negative F = open (' test4 ', ' rb+ ') F.tell () #0f. Read () #b ' \xe6\xb5\x8b\xe8\xaf\x95 ' F.tell () #6f. Write (b ' ABC ') f.seek (0) #0f. Seek (2,1) #2f. Read () #b ' \x8b\xe8\xaf\x95abc ' F.seek ( -2,1) #7f. Seek (2,2) #9f. Seek (0) #0f. Seek (- 2,2) #f. Read () # ' BC ' F.seek ( -20,2) #Error invaild argumentf.close () buffering: Buffer-1 represents the buffer that makes the default size, if it is fine mode, Using the Io.defaults_buffer_size value, the default is 4096 or 8192 if it is a text mode, if it is a terminal device, is the row cache mode, if not the use of binary strategy 0 is used in binary mode only, indicating that the pipe buffer 1 is only used in text mode,  means to use row buffering, meaning to see a newline character flush greater than 1 to specify buffer size buffer buffer: Buffer A memory space, generally a FIFO queue, until the buffer is full or the threshold is reached, the data will flush to disk flush () Flush () IO is called before the buffer data is written to disk close (). Default_buffer_size The default buffer size, byte >>>import io>>>io. default_buffer_size>>>8192buffering = 1 T and b, both are IO. default_buffer_sizebuffering = 0b off buffer, t support buffering = 1b on a byte, T row buffer, encounter newline characters flushbuffering > 1b mode indicates buffer size. The value of the buffer can exceed IO. Default_buffer_size until the set value is exceeded to flush the buffer. T mode, is IO. Default_buffer_size,flush completion of the current string is also written to disk Summary: 1, text mode, generally with the default buffer size 2, binary mode, is a byte operation, you can specify the size of buffer 3, the default buffer size is a better choice, Unless you know it clearly, no.Do not adjust 4, explicitly know the need to write disk, will call flush manually, instead of waiting for automatic flush or close when the context management: Ulimit-a View all restrictions, where open files is the limit of open file number, Default 1024 exception handling: When an exception occurs, the exception is intercepted, but because a lot of code can appear OSError exception, bad judgment exception, because the resource limit produces f = open (' Test ') try:f.write (' abc ') #文件只读, write failed fin Ally:f.close () #正确做法使用finally可以保证打开的文件可以被关闭上下文管理, a special syntax that is given to the interpreter to take the release file object using with ... The AS keyword context-managed statement block does not open when a new scope with a statement block finishes executing, automatically shuts down the file Object def fwith open (' Test ') as F:f.write (' Hello ') #文件只读, write failed f.closed (test F  is off)

  

python--i/o

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.