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: 通常使用utf8
  • errors:报错级别
  • 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。code

file = open("hello.txt", "r")
read1 = file.read()
print(read1)
file.close()

# 输出:hello world

write()方法

write()方法用于向文件中写入指定字符串。对象

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

file = open("hello.txt", "w")
file.write("hello world")
file.close()

writelines() 方法

writelines() 方法用于向文件中写入一序列的字符串。这一序列字符串能够是由迭代对象产生的,如一个字符串列表。换行须要制定换行符 n。文档

file = open("hello.txt", "w")
con = ["a \n", "b\n", "c\n"]
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() 方法用于从文件读取整行,包括 "n" 字符。若是指定了一个非负数的参数,则返回指定大小的字节数,包括 "n" 字符。it

# 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 则返回空字符串。coding

file = open("hello.txt", "r")
content = file.readlines()
print(content)
file.close()

# 输出:['123\n', '456\n', '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()
相关文章
相关标签/搜索