APP下载

python中file物件的常用方法

消息来源:baojiabao.com 作者: 发布时间:2024-05-09

报价宝综合消息python中file物件的常用方法

open() 方法

Python open() 方法用于开启一个档案,并返回档案物件,在对档案进行处理过程都需要使用到这个函式,如果该档案无法被开启,会丢掷 OSError。(使用 open() 方法一定要保证关闭档案物件,即呼叫 close() 方法)

open(file, mode='r', buffering=-1, encoding=None, errors=None, newline=None,

closefd=True, opener=None)

file:必需,档案路径(相对或者绝对路径)。mode: 可选,档案开启模式buffering: 设定缓冲encoding: 一般使用utf8errors:报错级别newline: 区分换行符closefd:传入的file引数型别

file物件的常用函式

close()方法

close()方法用于关闭一个已开启的档案,关闭后的档案不能再进行读写操作, 否则会触发 ValueError 错误。close() 方法允许呼叫多次。

语法:fileObject.close();

file = open("hello.txt","wb")

print(file.name)

file.close()

# 输出:hello.txt

read() 方法

read() 方法用于从档案读取指定的字节数,如果未给定或为负则读取所有。

首先自己建立一个hello.txt文件(自定义),文件中的内容是hello world。

file = open("hello.txt", "r")

read1 = file.read()

print(read1)

file.close()

# 输出:hello world

write()方法

write()方法用于向档案中写入指定字串。

向一个空的hello.txt文件中写入hello world。

file = open("hello.txt", "w")

file.write("hello world")

file.close()

writelines() 方法

writelines() 方法用于向档案中写入一序列的字串。这一序列字串可以是由迭代物件产生的,如一个字串列表。换行需要制定换行符 。

file = open("hello.txt", "w")

con = ["a ", "b ", "c "]

file.writelines(con)

file.close()

# hello.txt档案中写入

a

b

c

flush() 方法

flush() 方法是用来重新整理缓冲区的,即将缓冲区中的资料立刻写入档案,同时清空缓冲区,不需要是被动的等待输出缓冲区写入。

一般情况下,档案关闭后会自动重新整理缓冲区,但如果你需要在关闭前重新整理它,就可以使用 flush() 方法。

file = open("hello.txt", "wb")

print("档名:", file.name)

file.flush()

file.close()

# 档名: hello.txt

readline() 方法

readline() 方法用于从档案读取整行,包括 " " 字元。如果指定了一个非负数的引数,则返回指定大小的字节数,包括 " " 字元。

# hello.txt的内容

123

456

789

file = open("hello.txt", "r")

content = file.readline()

print(content)

file.close()

# 输出hello.txt档案的第一行:123

readlines() 方法

readlines() 方法用于读取所有行(直到结束符 EOF)并返回列表,该列表可以由 Python 的 for... in ... 结构进行处理。如果碰到结束符 EOF 则返回空字串。

file = open("hello.txt", "r")

content = file.readlines()

print(content)

file.close()

# 输出:['123 ', '456 ', '789']

seek() 方法

seek() 方法用于移动档案读取指标到指定位置。

fileObject.seek(offset[, whence])

offset表示开始的偏移量,也就是代表需要移动偏移的字节数。whence:可选,预设值为 0。给offset引数一个定义,表示要从哪个位置开始偏移;0代表从档案开头开始算起,1代表从当前位置开始算起,2代表从档案末尾算起。假设hello.txt档案中的内容是abcdefghijk,那么我们使用 seek() 方法来移动档案指标试试:

file = open("hello.txt", "r")

file.seek(3) #档案指标移动到第三位,从第四位开始读

print(file.read()) # 输出:defghijk

file.seek(5)

print(file.read()) # 输出:fghijk

file.close()

tell() 方法

tell() 方法返回档案的当前位置,即档案指标当前位置。

file = open("hello.txt", "r")

file.seek(4) #档案指标移动到第三位,从第四位开始读

print(file.tell()) # 4

file.close()

fileno()方法

fileno() 方法返回一个整型的档案描述符(file descriptor FD 整型),可用于底层操作系统的 I/O 操作。

file = open("hello.txt", "w")

con = file.fileno()

print(con)

file.close()

# 输出:3

isatty()方法

如果档案连线到一个终端装置返回 True,否则返回 False。

file = open("hello.txt", "w")

con = file.isatty()

print(con)

file.close()

# 输出:False

truncate() 方法

truncate() 方法用于截断档案,如果指定了可选引数 size,则表示截断档案为 size 个字元。 如果没有指定 size,则从当前位置起截断;截断之后 size 后面的所有字元被删除。

# hello.txt文字

a

b

c

d

e

file = open("hello.txt", "r")

con = file.readline()

print(con) # 输出:a

file.truncate() # 截断剩下的字串

con = file.readline()

print(con)

file.close()

2019-07-25 23:55:00

相关文章