Tag: Obj receive indicates network stream buffering specific users Erro condition
Read file:
Try: f = open ('/path/to/file ', ' R ') print (F.read ()) Finally: if f: f.close ()----------------------- --------------with open ('/path/to/file ', ' R ') as F: print (F.read ())------------------------------------- Call ReadLine () can read a line of content at a time, call ReadLines () to read all of the content one at a time and return by rows LISTF = open ('/path/to/file ', ' r ') for lines in F.readlines (): Print (Line.strip ()) # Erase the end of ' \ n '
Write a file
Writing and reading files are the same, the only difference being that when a function is called, an identifier is passed in or a write open() - ‘w‘ ‘wb‘ in file or a binary file is written:
f = open ('/users/michael/test.txt ', ' W ') f.write (' Hello, world! ') F.close ()
---------------------------
with open(‘/Users/michael/test.txt‘, ‘w‘) as f: f.write(‘Hello, world!‘)
To write to a specific encoded text file, pass in the parameter to the open() function encoding and automatically convert the string to the specified encoding.
File-like Object
Like the open() function returned by this kind read() of object with a method, in Python is called File-like object. In addition to file, it can be memory byte stream, network stream, custom stream and so on. File-like object does not require inheritance from a particular class, just write a read() method.
StringIOFile-like Object, which is created in memory, is commonly used as a temporary buffer.
binary files
The default is to read the text file, and it is a UTF-8 encoded text file. To read a binary file, compare slices, videos, and so on, open the file in a ‘rb‘ mode:
>>> f = open ('/users/michael/test.jpg ', ' RB ') >>> f.read () b ' \xff\xd8\xff\xe1\x00\x18exif\x00\x00 ... ' # Hexadecimal representation of bytes
Character encoding
To read a text file that is not UTF-8 encoded, you need to pass in parameters to the open() function encoding , for example, to read the GBK encoded file:
>>> f = open ('/users/michael/gbk.txt ', ' R ', encoding= ' GBK ') >>> f.read () ' Test '
Encountered UnicodeDecodeError because some illegal encoded characters may be mixed in the text file. In this case, the open() function also receives a errors parameter,
Indicates what to do if a coding error is encountered. The simplest way is to ignore it directly:
f = open ('/users/michael/gbk.txt ', ' R ', encoding= ' GBK ', errors= ' ignore ')
Write Memory Stringio
getvalue()method is used to obtain the post-write Str.
From io Import stringiof = Stringio () f.write (U ' Hello ') f.write (U ") f.write (U ' World ') print f.getvalue ()
要读取StringIO,可以用一个str初始化StringIO,然后,像读文件一样读取:
>>> from io import StringIO>>> f = StringIO(‘Hello!\nHi!\nGoodbye!‘)>>> while True:... s = f.readline()... if s == ‘‘:... break... print(s.strip())...Hello!Hi!Goodbye!
Bytesio implements read-write bytes in memory, we create a bytesio and then write some bytes:
>>> from IO import bytesio>>> f = Bytesio () >>> f.write (' Chinese '. Encode (' Utf-8 ')) 6>>> Print (F.getvalue ()) B ' \xe4\xb8\xad\xe6\x96\x87 '
The Bytesio can be initialized with a bytes and then read like a read file
>>> from IO import bytesio>>> f = Bytesio (b ' \xe4\xb8\xad\xe6\x96\x87 ') >>> f.read () b ' \xe4\ Xb8\xad\xe6\x96\x87 '
Re-learning the Python series (iii)? WTF?