打开文件, 用于打开文件并返回一个文件对象
源码:
def open(name, mode=None, buffering=None): # real signature unknown; restored from __doc__
"""
open(name[, mode[, buffering]]) -> file object
Open a file using the file() type, returns a file object. This is the
preferred way to open a file. See file.__doc__ for further information.
"""
return file('/dev/null')
open 函数返回的只是一个文件对象
读写文件需要调用 read 函数和 write 函数,完成读写后,需要调用 close 函数。
read 函数可以带参数,指定读取的字节数,不带参数则默认读取文件全部内容。
write 函数参数为字符串类型,即写入文件的内容。
参数含义:
- name:文件名称
- mode:模式
- buffering:缓冲
name:名称
例子:
mode:模式
其他文件模式参数还有:
参数 |
说明 |
r |
读模式 |
a |
追加模式 |
b |
二进制模式 |
+ |
读写模式 |
例子:
# 打开文件,写内容
f = open("C:/Users/admin/PycharmProjects/selenium_framework_demo/testOpenFiles.txt", "w")
f.write("写入新内容")
#打开文件,读内容
f = open("C:/Users/admin/PycharmProjects/selenium_framework_demo/testOpenFiles.txt", "r")
print(f.read())
f.close()
这个办法会把原来的内容覆盖掉,如果想要不覆盖,我们用追加模式就好了
# 打开文件,写内容
f = open("C:/Users/admin/PycharmProjects/selenium_framework_demo/testOpenFiles.txt", "a")
f.write("\追加新的内容")
#打开文件,读内容
f = open("C:/Users/admin/PycharmProjects/selenium_framework_demo/testOpenFiles.txt", "r")
print(f.read())
f.close()
二进制模式,则有必要说明一下。在没有特别说明的情况下,Python 处理的文件默认是文本文件,如果处理二进制文件(如声音或者图像),则需要在模式参数中增加 b,如 rb 可用于读取一个二进制文件。
buffering:缓冲
open 函数的第三个参数控制着文件缓冲,
参数为0(或者 False),对文件读写操作都是无缓冲的,直接读写硬盘;
参数为1(或者 True),对文件的读写操作就是有缓冲的,数据将缓冲于内存中,只有当调用 flush 或者 close 函数时,才会更新硬盘上的数据。
需要说明的是,该参数可以大于1(代表缓冲区的大小,单位为字节),也可以为负数(代表使用默认的缓冲区大小)。
read 函数可以带参数,指定读取的字节数,不带参数则默认读取文件全部内容
例子:
#打开文件,在现有文件后追加信息并换行
f = open("D:/Users/data.txt",'a')
f.write('\nappend information')
f.close()
#打开文件,读取文件前4个字节
f = open("D:/Users/data.txt",'r')
print(f.read(4))
#关闭文件
f.close()
结果:
app
file.readline(),读取文件中单独的一行内容(从当前 位置开始直到一个换行符出现)
不带参数时,默认读取一整行;
带参数(非负整数)则表示 readline 可以读取当前行内容的最大字节数。
此外,可以通过 readline 函数来遍历文件
#打开文件,写入信息并换行
f = open("D:/Users/data.txt",'w')
f.write('append information1\n')
f.write('append information2\n')
f.write('append information3\n')
f.close()
#打开文件,按行读取文件内容
f = open("D:/Users/data.txt",'r')
while True:
line = f.readline()
#读取完毕退出
if(not line):
break
print('content:',line)
#关闭文件
f.close()
执行结果
content: append information1
content: append information2
content: append information3
带参数:
#打开文件,并写入信息
f = open("D:/Users/data.txt",'w')
f.write('append information1\n')
f.write('append information2')
f.close()
#打开文件,按行及参数限制读取文件内容
f = open("D:/Users/data.txt",'r')
while True:
line = f.readline(10)
#读取完毕退出
if(not line):
break
print('content:',line)
#关闭文件
f.close()
结果:
content: append inf
content: ormation1
content: append inf
content: ormation2
readline 函数每次读取一行,与之对应的 readlines 则可一次读取整个文件的所有行。如下实例:
#打开文件,并写入信息
f = open("D:/Users/data.txt",'w')
f.write('append information1\n')
f.write('append information2')
f.close()
#打开文件,并一次读取所有行
f = open("D:/Users/data.txt",'r')
for line in f.readlines():
print('content:',line)
#关闭文件
f.close()
结果:
content: append information1
content: append information2
通过 writelines 函数。可以一次向文件写入多行内容,如下实例:
#打开文件,一次写入多行内容
f = open("D:/Users/data.txt",'w')
content = ['append information1\n','append information2']
f.writelines(content)
f.close()