Python-- file read and write

First, the document Open

 

Open

Pointer location

File types to read

r

Read-only (the default mode)

Pointer at the beginning of the file, the file does not exist error

1. The default read files are ASCII encoded text files

2. If the file is read binary files, such as pictures, video requires the use of 'rb' mode

3. The read non- ASCII -encoded text file must be opened in binary mode, re-decoded

f=open('E:/test.txt','rb')

f.read().decode('gbk')

 

rb

Read-only binary format

Pointer at the beginning of the file, the file does not exist error

r+

Read and write

Pointer at the beginning of the file, the file does not exist error

rb+

Binary format readable and writable

Pointer at the beginning of the file, the file does not exist error

w

just write

File exists, it is overwritten; does not exist, create

wb

Write only binary format

File exists, it is overwritten; does not exist, create

w+

Read and write

File exists, it is overwritten; does not exist, create

wb+

Binary format readable and writable

File exists, it is overwritten; does not exist, create

a

add to

File exists, an increase in the end of the file pointer; does not exist, it is created

from

Additional binary format

File exists, an increase in the end of the file pointer; does not exist, it is created

a+

Read and write

File exists, an increase in the end of the file pointer; does not exist, it is created

ab + _

Binary format readable and writable

File exists, an increase in the end of the file pointer; does not exist, it is created

 

Second, the open file method

 

For example

file does not exist

You need to call the use Close () method

note

Python built- open function

f=open('E:/test.txt','r')

File does not exist it will IOError

Must be written to call close ()

1. file object will occupy the operating system resources, and the operating system at the same time the number of files that can be opened is limited

2. When writing a file, the operating system does not write data to disk immediately, but let the contents of the cache, and then write idle, only to call close () when the method, data manipulation will be no data will be written, disk. Do not write close () method it is possible to write only a part of the data to disk, the remaining data is lost

3. The file does not exist, you can use the try ... a finally avoided, but with more trouble

 

With Statement

with open('E:/test.txt','r') as f:

File does not exist it will IOError

with statement automatically calls the close () method

Third, file read and write operations targeting

 

method

usage

note

read

f.read(size)

If the size is not reached, f.read i.e. () to read the entire contents of disposable file, return type str; if size i.e. f.read (50) up to the content read byte

1. The file is very small, can be read one time and file size, memory size exceeds content explode

2. The file is too large, the available f.read (size), x for each read up to size bytes of content

3. If the configuration file, call readlines () the most convenient

f.readline()

Each read a single line , the return type str

f.readlines()

Time to read all the contents and press OK to return list

write

f.write()

写文件

将字符串写入文件,如果写入结束,需要在字符串后面加上"\n"

定位

f.seek(offset,from_what)

f.seek(2,0)读取位置向后移动2个字符

from_what值为文件的开始位置,可以省略;offset为偏移量

当前位置

f.tell()

f.tell(),返回当前文件位置

 

四、读取非ASCII文件,如utf-8,gbk类型

方法1:读取时指定类型encoding='utf-8'

with open('E:/test1.txt','r',encoding='utf-8') as f

    print(f.read())

结果:

你好

方法2:

必须:1.以二进制模式打开,再解码

codecs模块:读文件时自动转换编码,直接读出unicode

import codecs

with codecs.open('E:/test.txt','rb','utf-8') as f:

f.read()

注意:有些编码不规范,会遇到UnicodeDecodeError,可能文件中有些非法编码的字符,遇到这种情况,可用open()函数可以接收errors参数,表示如果遇到编码错误后如何 处理,最简单方式是直接忽略f=open('E:/test1.txt','r',encoding='gbk',errors='ignore')

五、File对象的属性 

f.closed

文件已关闭返回true,否则返回false

f.mode

返回被打开文件的访问模式

f.name

返回文件名称

 

Guess you like

Origin www.cnblogs.com/yezishen/p/11783587.html