python之文件操作
python之文件操作
文件
文件(File),计算机中一切皆文件。
通过python程序对各种文件进行增删查改。
I/O(Input / Output)
文件操作步骤:
- 打开文件
- 查找、修改文件
- 关闭文件
打开文件
# 使用open()函数打开文件
# def open(
# file: _OpenFile,
# mode: OpenTextMode = ...,
# buffering: int = ...,
# encoding: str | None = ...,
# errors: str | None = ...,
# newline: str | None = ...,
# closefd: bool = ...,
# opener: _Opener | None = ...,
# )
# open(file,more='r',buffering = -1,encoding= None,...),open()函数有返回值
# 创建文件名变量
# 文件目录需要注意,在windows可以使用/代替\,也可以使用原始字符串r
# 文件在当前目录
file_name = 'test.txt'
# 文件在下级目录
file_name = 'file_test\\test-1.txt'
# 文件在上级目录可以使用..切换目录
file_name = '../test-2.txt'
# 使用绝对路径,从根目录开始写
file_name = r'C:\Users\WZS\Desktop\test-3.txt'
# 使用open()函数打开文件,返回一个文件对象,使用file_obj接收
file_obj = open(file_name)
# 打印文件对象
print(file_obj)
关闭文件
1.使用close()方法关闭文件
# 关闭文件,文件操作后必须进行关闭
# 1.使用close()方法关闭文件
file_name = r'C:\Users\WZS\Desktop\test-3.txt'
# 使用open()函数打开文件,返回一个文件对象,使用file_obj接收,通过文件对象进行操作,读取,写入
file_obj = open(file_name)
# 读取内容,read()方法用来读取文件内容,将内容作为字符串返回,需要使用变量接收
content = file_obj.read()
# 打印内容
print(content)
# 使用close()方法进行关闭,使用文件对象
file_obj.close()
2.使用with ... as 语句打开关闭文件
# 使用with ... as 语句关闭文件,在with结束自动关闭文件
file_name = 'test.txt'
try:
with open(file_name) as file_obj:
content = file_obj.read()
print(content)
except FileNotFoundError:
print(f'{file_name} 这个文件不存在')
文件的读取
# 文件读取需要注意文件类型,
# 文件类型纯文本文件,二进制文件
# 默认是以文本文件打开编码默认为None,如果编码格式不对会报错,需要修改encoding='utf-8'
# 调用read()方法会一次性读取,导致大文件缓慢和内存泄露可能,read()可以接受size作为参数,指定读取的字符的数量,默认值为-1,即读取所有字符,每次读取从上次的位置开始读取
file_name = 'test.txt'
try:
with open(file_name,mode='r',encoding='utf-8') as file_obj:
content = file_obj.read(6)
# content = file_obj.read(6)
print(content)
except FileNotFoundError:
print(f'{file_name} 这个文件不存在')
print(len(content))
读取大文件
# 定义每次读取的大小
file_name = 'test.txt'
try:
with open(file_name,mode='r',encoding='utf-8') as file_obj:
chuck = 20
while True:
content = file_obj.read(chuck)
if not content:
# 内容读取完毕
break
print(content)
except FileNotFoundError:
print(f'{file_name} 这个文件不存在')
# radline方法,每次读取一行数据
with open(file_name,mode='r',encoding='utf-8') as file_obj:
print(file_obj.readline(),end='')
# readlines方法,每次读取一行,存在一个列表中
import pprint
with open(file_name,mode='r',encoding='utf-8') as file_obj:
r = file_obj.readlines()
pprint.pprint(r[0])
文件的写入
# 定义文件名称
file_name = 'test.txt'
# 使用open函数打开文件,使用.write方法进行写入
# 指定操作模式,r-读,w-写,a-追加,文件不存在则创建文件,返回值为字符个数
with open(file_name,mode='w',encoding='utf-8') as file_obj:
file_obj.write('He llo world!\n')
file_obj.write('1111\n')
file_obj.write('22222\n')
# 二进制文件操作
file_name = 'C:/Users/WZS/Music/古巨基 - 恋无可恋.flac'
# 读取模式
# t 读取文本文件(默认值)
# b 读取二进制文件
with open(file_name,mode='rb') as file_obj:
# 定义一个新的变量接收新的文件
new_name = 'test.flac'
# 定义写入新的文件
with open(new_name,'wb') as new_obj:
# 定义每次读取的字节大小
chuck = 1024 * 100
# 执行循环写入
while True:
content = file_obj.read(chuck)
# 文件写入完成跳出循环
if not content:
break
# 写入到新的文件对象中
new_obj.write(content)
# 文件读取的位置
file_name = 'test.txt'
with open(file_name,'rb') as file_obj:
# print(file_obj.read(100))
# seek方法修改当前读取的位置,seek(要切换的位置,计算位置的方式:0-从头计算,1-从当前位置计算,2-从最后位置开始计算)
file_obj.seek(10)
print(file_obj.read(20))
# tell()方法用来查看当前读取的位置
print(file_obj.tell())
点点滴滴,积少成多,终有一日能发挥用处。

浙公网安备 33010602011771号