Python: open () function

python open () function is used to open a file, create a file object associated method can invoke it to read and write.

More file operations refer to: Python file I / O.
Function Syntax

open(name[, mode[, buffering]])

Parameter Description:

name : 一个包含了你要访问的文件名称的字符串值。

mode : mode 决定了打开文件的模式:只读,写入,追加等。所有可取值见如下的完全列表。这个参数是非强制的,默认文件访问模式为只读(r)。

buffering : 如果 buffering 的值被设为 0,就不会有寄存。如果 buffering 的值取 1,访问文件时会寄存行。如果将 buffering 的值设为大于 1 的整数,表明了这就是的寄存区的缓冲大小。如果取负值,寄存区的缓冲大小则为系统默认。

Open different modes complete list of file:
Mode Description
r open read-only file. Pointer file will be placed at the beginning of the file. This is the default mode.
rb Open a binary file for read-only format. The file pointer will be placed at the beginning of the file. This is the default mode.
r + open a file for reading and writing. The file pointer will be placed at the beginning of the file.
rb + open a file for reading and writing binary format. The file pointer will be placed at the beginning of the file.
w opens a file for writing only. If the file already exists then open the file and start editing from the beginning, that is, the original content will be deleted. If the file does not exist, create a new file.
wb open a file for writing in binary format only. If the file already exists then open the file and start editing from the beginning, that is, the original content will be deleted. If the file does not exist, create a new file.
w + open a file for reading and writing. If the file already exists then open the file and start editing from the beginning, that is, the original content will be deleted. If the file does not exist, create a new file.
wb + opens a file for reading and writing binary format. If the file already exists then open the file and start editing from the beginning, that is, the original content will be deleted. If the file does not exist, create a new file.
Open a file for append. If the file already exists, the file pointer will be placed at the end of the file. In other words, the new content will be written after the existing content. If the file does not exist, create a new file for writing.
ab open a file in binary format for additional. If the file already exists, the file pointer will be placed at the end of the file. In other words, the new content will be written after the existing content. If the file does not exist, create a new file for writing.
a + open a file for reading and writing. If the file already exists, the file pointer will be placed at the end of the file. It would append mode when the file is opened. If the file does not exist, create a new file for reading and writing.
ab + opens a file for additional binary format. If the file already exists, the file pointer will be placed at the end of the file. If the file does not exist, create a new file for reading and writing.
file object methods

file.read([size]):size 未指定则返回整个文件,如果文件大小 >2 倍内存则有问题,f.read()读到文件尾时返回""(空字串)。

file.readline():返回一行。

file.readlines([size]) :返回包含size行的列表, size 未指定则返回全部行。

for line in f: print line :通过迭代器访问。

f.write("hello\n"):如果要写入字符串以外的数据,先将他转换为字符串。

f.tell():返回一个整数,表示当前文件指针的位置(就是到文件头的比特数)。

f.seek(偏移量,[起始位置]):用来移动文件指针。
    偏移量: 单位为比特,可正可负
    起始位置: 0 - 文件头, 默认值; 1 - 当前位置; 2 - 文件尾

f.close() 关闭文件

Guess you like

Origin blog.csdn.net/weixin_44523387/article/details/93370666