File read and write, StringIO, BytesIO
- IO Programming: https://www.liaoxuefeng.com/wiki/1016959663602400/1017606916795776
- - - - File reading and writing: read and write file request is the operating system to open a file object (commonly referred to as a file descriptor), then the interface provided by the operating system reads data (file read) from the object file, or write the data into this file object (file write).
- Reading file: python using the built-in function open (), passing the file name, and identifiers
Code:
`` `
# read file, an object file is Readme.txt, 'r' denotes a read-only, it will be thrown if the file does not exist IOError
F = Open ( 'Readme.txt', 'r')
Content = F. read () # read the entire contents of a one-time file, python read the contents of memory, the object is represented by a str
Print (content)
f.close () # close the file, the file must be closed after use, because the file objects occupancy of the operating system resource
# read files trilogy, 1. 2. open the file read the file 3. Close the fileIOError: due to file read and write are likely to produce IOError, once the error, back f.close () will not be called
try:
f1 = open('read.txt','r')
print(f1.read())
finally:
if f1:
f1.close()# With: every so realistic in too cumbersome, so, Python was introduced with the statement to help us automatically call close () method
with open ( 'read.txt', ' r') as f: # in front of it and try ... finally is the same, but the code is simple better, and not have to call f.close () method.
print (f.read ())# Read (size) - read 1 byte content
with Open ( 'Readme.txt', 'R & lt') AS F:
# 4 bytes read
print (f.read (4))#readline () - read the contents of a row
with Open ( 'Readme.txt', 'R & lt') AS F:
Print (f.readline ())# Readlines () - one read row by row all content returned List
with Open ( 'Readme.txt', 'R & lt') AS F:
linelst f.readlines = ()
for Line in linelst:
Print (line.strip () )# Binary files: read binary files, such as pictures, video, etc., open the file with 'rb' mode
# Coding error handling incoming errors
with Open ( 'image.png', 'rb') AS f:
Print (f.read ())
# Character Encoding : reading non UTF-8 encoded text file, it is necessary to open () function passing the encoding parameter, e.g., read GBK encoded files:
with Open ( 'gbk.txt', 'R & lt', encoding = 'GBK' ) AS f:
Print (f.read ())
F = Open ( '/ the Users / Michael / gbk.txt', 'R & lt', encoding = 'GBK', errors = 'the ignore')
`` `- operation result:
- Write file:
Code:
`` `
# write file: file read and write files is the same, the only difference is that the call to open () function, the incoming identifier 'w' or 'wb' for write text files or write binary files
w = open ( 'write.txt', 'w' ) # 'w' will create the file when no files have a file will overwrite the original file
w.write ( 'the Hello')
w.close ()# 'a' 文件末尾追加内容
# 写特定编码的文件
with open('write.txt','a') as w: # as w 即 w = with open('write.txt','a')
w.write('python3')
w = open('write.txt','w',encoding='gbk')
w.write('gbk')
w.close()
```
- Reading file: python using the built-in function open (), passing the file name, and identifiers
- StringIO: 在内存中读写str
代码:
# 读
```
# String IO:在内存中读写str
from io import StringIO
f = StringIO()
print(f.write('hello')) # 打印字节数
print(f.getvalue()) # getvalue()获取内存中的str
r = StringIO('hello\nworld!')
while True:
s = r.readline()
if s == '':
break
print(s.strip()) # 替换换行 \n
```
- BytesIO:BytesIO实现了在内存中读写bytes
Code:
`` `
# BytesIO: operating binary data
from IO Import BytesIO
W = BytesIO ()
# note that writing is not str, but after the UTF-8 encoded bytes.
print (w.write ( 'good luck to!'. encode ( 'utf- 8'))) # Bytes printing
print (w.getvalue ()) # acquires binary value# 读
r = BytesIO(b'\xe5\xa5\xbd\xe8\xbf\x90\xe6\x9d\xa5\xef\xbc\x81')
print(r.read())
```