open() 方法
Python open() 方法用于打开一个文件,并返回文件对象,在对文件进行处理过程都需要使用到这个函数,如果该文件无法被打开,会抛出 OSError。
注意:使用 open() 方法一定要保证关闭文件对象,即调用 close() 方法。
open() 函数常用形式是接收两个参数:文件名(file)和模式(mode)。
open(file, mode=‘r’)
file 对象
file 对象使用 open 函数来创建,下表列出了 file 对象常用的函数:
①
file.close()
close() 方法用于关闭一个已打开的文件。关闭后的文件不能再进行读写操作, 否则会触发 ValueError 错误。 close() 方法允许调用多次。
#打开文件
f=open("aaa.text","w")
print("文件名为:",f.name)
f.close()#关闭文件
②
file.flush()
flush() 方法是用来刷新缓冲区的,即将缓冲区中的数据立刻写入文件,同时清空缓冲区,不需要是被动的等待输出缓冲区写入。
#打开文件
f=open("aaa.text","w")
print("文件名为:",f.name)
f.flush() #刷新缓冲区
f.close()#关闭文件
③
file.fileno()
fileno() 方法返回一个整型的文件描述符(file descriptor FD 整型),可用于底层操作系统的 I/O 操作。
语法
#打开文件
f=open("aaa.text","w")
print("文件名为:",f.name)
fid=f.fileno()
print("文件描述为:",fid)
f.close()#关闭文件
④
file.isatty()
isatty() 方法检测文件是否连接到一个终端设备,如果是返回 True,否则返回 False。
#打开文件
f=open("aaa.text","w")
print("文件名为:",f.name)
ret=f.isatty()
print("返回值:",ret)
f.close()#关闭文件
⑤
file.read()
read() 方法用于从文件读取指定的字节数,如果未给定或为负则读取所有。
文件aaa.txt 的内容改为如下:
# 打开一个文件
f=open("aaa.txt","r+")
print("文件名为:",f.name)
line=f.read(10)
print("读取的字符串: %s"%(line))
# 关闭打开的文件
f.close()
read(10)只读入10个字符
⑥
file.readline()
readline() 方法用于从文件读取整行,包括 “\n” 字符。如果指定了一个非负数的参数,则返回指定大小的字节数,包括 “\n” 字符。
文件aaa.txt 的内容改为如下:
# 打开一个文件
f=open("aaa.txt","r+")
print("文件名为:",f.name)
line=f.readline()
print("读取第一行 %s" % (line))
line=f.readline(5)
print("读取的字符串为:%s" % (line))
# 关闭打开的文件
f.close()
⑦
file.readlines()
readlines() 方法用于读取所有行(直到结束符 EOF)并返回列表,该列表可以由 Python 的 for… in … 结构进行处理。 如果碰到结束符 EOF 则返回空字符串。
如果碰到结束符 EOF 则返回空字符串。
# 打开一个文件
f=open("aaa.txt","r+")
print("文件名为:",f.name)
for line in f.readlines():
line=line.strip() #strip用于移除字符串头尾指定的字符(默认为空格或换行符)或字符序列
print("读取的数据为:%s"%(line))
# 关闭打开的文件
f.close()
⑧
file.seek
file.seek(offset[, whence]):
- offset : 开始的偏移量,也就是代表需要移动偏移的字节数,如果是负数表示从倒数第几位开始。
- whence:可选,默认值为 0。给 offset 定义一个参数,表示要从哪个位置开始偏移;0 代表从文件开头开始算起,1 代表从当前位置开始算起,2 代表从文件末尾算起
f=open("aaa.txt","rb+")
f.write(b'lalala1623')
print(f.seek(5)) #移动到文件的第六个字节
print(f.read(1))
print(f.seek(1))
print(f.seek(-3,2))#移动到文件的倒数第三字节
print(f.read(1))
f.close()
# 打开一个文件
f=open("aaa.txt","r+")
print("文件名为:",f.name)
line=f.readline()
print("读取的数据为:%s" % (line))
f.seek(0,0) ## 重新设置文件读取指针到开头
line=f.readline()
print("读取的数据为:%s"% (line))
# 关闭打开的文件
f.close()
⑨
file.tell()
tell() 方法返回文件的当前位置,即文件指针当前位置。
# 打开一个文件
f=open("aaa.txt","r+")
print("文件名为:",f.name)
line=f.readline()
print("读取的数据为:%s" % (line))
#获取当前位置
pos=f.tell()
print("当前位置为:%s" % (pos))
# 关闭打开的文件
f.close()
⑩
file.truncate()
truncate() 方法用于从文件的首行首字节开始截断,截断文件为 size 个字节,无 size 表示从当前位置截断;截断之后 V 后面的所有字节被删除,其中 Widnows 系统下的换行代表2个字节大小。
# 打开一个文件
f=open("aaa.txt","r+")
print("文件名为:",f.name)
line=f.readline()
print("读取的数据为:%s" % (line))
f.truncate()
line=f.readlines()
print("读取行:%s"% (line))
# 关闭打开的文件
f.close()
# 打开一个文件
f=open("aaa.txt","r+")
print("文件名为:",f.name)
line=f.readline()
print("读取的数据为:%s" % (line))
f.truncate(10) #截取10个字节
str=f.read()
print("读取数据:%s" %(str))
# 关闭打开的文件
f.close()
⑪
file.write()
write() 方法用于向文件中写入指定字符串。
在文件关闭前或缓冲区刷新前,字符串内容存储在缓冲区中,这时你在文件中是看不到写入的内容的。
如果文件打开模式带 b,那写入文件内容时,str (参数)要用 encode 方法转为 bytes 形式,否则报错:TypeError: a bytes-like object is required, not ‘str’。
# 打开一个文件
f=open("aaa.txt","r+")
print("文件名为:",f.name)
str="6:www.csdn.net"
# 在文件末尾写入一行
f.seek(0,2)
line=f.write(str)
f.seek(0,0)
for index in range(6):
line=next(f)
print("文件行号 %d - %s" % (index, line))
# 关闭打开的文件
f.close()
⑫
file.writelines()
writelines() 方法用于向文件中写入一序列的字符串。
这一序列字符串可以是由迭代对象产生的,如一个字符串列表。
换行需要制定换行符 \n。
# 打开一个文件
f=open("aaa.txt","w")
print("文件名为:",f.name)
seq=['lalala\n','hhhhh\n']
f.writelines(seq)
# 关闭打开的文件
f.close()
打开文件: