Three kinds of python file open mode

Three file open mode

Base mode operation consists of three files (default operating mode is the mode r):

  • r read mode
  • mode write w
  • a mode append

Read the contents of the file format has two kinds (the default mode to read and write content for b mode):

  • t text mode
  • b mode bytes

Note that: t, b maybe modes can not be used alone, and r are required mode, W mode, one of a mode in conjunction.

First, the file open mode (r Mode)

r: read, read-only mode and can only be read but not write, being given file does not exist.

f = open('F:\python自学\db.txt',mode='rt',encoding='utf-8')
data = f.read()
print(data)
print(f"type(data):{type(data)}")
f.close()

#输出:
name,123,1000

type(data):<class 'str'>
# rb:read by bytes
f = open('F:\python自学\db.txt',mode='rb')
data = f.read()
print(data)
print(f"type(data):{type(data)}")
f.close()

#输出:
b'name,123,1000\r\nname,123,1000000000\r\nname,123,100000\r\nchen,456,1222555444556\r\nchen,456,456\r\n'
type(data):<class 'bytes'>

f.read () to read the file command will go to the end of the file, if one read, will read a space.

f = open('F:\python自学\db.txt',mode='rt',encoding='utf-8')
data1 = f.read()
data2 = f.read()

print('data1:',data1)
print('data2:',data2)
f.close()

#输出:
data1: name,123,1000
name,123,1000000000
name,123,100000
chen,456,1222555444556
chen,456,456

data2: 

Since all content f.read () one time to read the file, if the file is very large, it may cause memory ringing off the hook, that is, computer stuck. It can be used f.readline () or f.readlines () to read the contents of the file.

# f.readline()/f.readlines()
f = open('F:\python自学\db.txt',mode='rt',encoding='utf-8')
data1 = f.readline()
data2 = f.readlines()
print('data1:',data1)
print('data2:',data2)
f.close()
#输出:
data1: name,123,1000

data2: ['name,123,1000000000\n', 'name,123,100000\n', 'chen,456,1222555444556\n', 'chen,456,456\n']

Second, the open mode of the file mode w

w: only write, can not read, there is time after the document will clear the file write content; file does not exist when the content is written after the file is created.

#wt
f = open('32w.txt',mode='wt',encoding='utf-8')
print('f.readable()',f.readable())# 判断文件是否可读
f.write('chenshuaibi\n')
f.write('what,厉害')
f.write('chen,佩服啊')
f.flush()#刷新,立刻将文件内容刷到硬盘
f.close()

#输出:
f.readable() False
#wb,编码成bytes类型
f = open('33w.txt',mode='wb')
f.write('chenshuaibi'.encode('unicode_escape'))
print("type('chenshuaibi'.encode('unicode_escape'))",type('chenshuaibi\n'.encode('unicode_escape')))
f.close()

#输出:
type('chenshuaibi'.encode('unicode_escape')) <class 'bytes'>

Third, open the file of a pattern

a: you can append. File exists, write to the end of the file; the file does not exist when the content is written after the file is created.

#at
f = open('35a.txt',mode='at',encoding='utf-8')
print("f.readable()",f.readable())
f.write('chen真帅')
f.close()
#输出:
f.readable() False


Fourth, read a binary file opened

b mode is a common pattern, because all files are stored in the hard disk binary form, should be noted that: b mode read and write files, must not add encoding parameters, can not be performed because the binary coding

try:
    import requests

    response = requests.get(
        'http://www.chenyoude.com/Python从入门到放弃/文件的三种打开模式-mv.jpg?x-oss-process=style/watermark')
    data = response.content

    f = open('mv.jpg?x-oss-process=style/watermark', 'wb')
    f.write(data)
    print('done...')
    f.close()
except Exception as e:
    print(e, '报错了,那就算了吧,以后爬虫处会详细介绍')

done...

f = open('34w.txt', 'wb')
f.write('nick 好帅啊'.encode('utf8'))
f.close()

Guess you like

Origin www.cnblogs.com/chenziqing/p/11321087.html