Python文件操作

打开方式:

​ 基础模式: w r x a
​ w模式:write 写入
​ 如果文件不存在则创建
​ 文件存在则打开,并清空文件内容
​ 文件打开后,指针在文件的最前面

​ r模式:read 读取
​ 1.如果文件不存在会报错
​ 2.如果文件存在则打开
​ 3.指针在文件最前面
​ x模式:xor 异或模式
​ 1.如果文件不存在则创建文件
​ 2.如果文件存在则报错(防止覆盖)
​ 3.文件的指针在文件的最前面
​ a+模式:append 追加模式
​ 1.文件不存在则创建
​ 2.文件存在则打卡
​ 3.文件的指针在文件最后位置

​ w+模式:先写入后清空

​ r+模式:即可读也可写

参数:
encoding 可选参数,设置文件的字符集
如果是二进制的文件,不需要设置字符集
设置方法:encoding='utf-8'

文件操作相关函数

文件.open()打开文件

格式:open(文件路径,打开模式,[字符集])

# file.open() 打开文件
fp=open('./1.txt','w',encoding='utf-8')
print(fp,type(fp))

文件.seek()设置指针位置

格式:file.Seek(0,2) 把文件指针设置到文件终止位置

​ fp.seek(6) 设置文件指针的起始位置,按照字节数计算

​ fp.seek(0) 设置文件指针到文件的开头

文件.write()写入内容

格式:file.write(字符串)

fp=open('./1.txt','w',encoding='utf-8')
print(fp,type(fp))
# file.write() 写入文件
fp.write('hello world')
# 关闭文件file.close()
fp.close()

# 追加写入a
fp=open('./1.txt','a',encoding='utf-8')
# 写入
fp.write('\npython') # 'hello python'
fp.close()

文件.read()读取内容

格式:file.read() 从当前指针读到最后

格式:file.read(读取的字节数) 可指定读取文件内容的起始位置.

# 读取文件
fp=open('./1.txt','r',encoding='utf-8')
res=fp.read()
print(res)
fp.close()

文件.readline()读取一行

格式:file.readline()一次只读取一行

格式:file.readline(3) 读取当前行3个字符

with open('./txt/3.txt','r',encoding='utf-8') as fp:
    res=fp.readline()
    print(res) # 床前明月光
    res=fp.readline()
    print(res) # 疑是地上霜
    res=fp.readline(3)

文件.readlines()读取多行

格式:file.readlines() 读取多行到列表中

with open('./txt/3.txt','r',encoding='utf-8') as fp:
    res=fp.readlines()
    print(res) 

文件.close()

格式:file.close()关闭打开的文件

fp=open('./1.txt','w',encoding='utf-8')
fp.close()

文件操作简略写法:

# 文件操作高级语法
"""
with open(文件路径,打开模式) as 变量:
    变量.操作()
"""
# w+ 先清空后写入
with open('./1.txt','w+') as fp:
    res=fp.read()
    print('w+===',res) # w+===
    fp.write('hello python and java')
    res=fp.read()
    print(res)
# r+ 即可读也可写
with open('./1.txt','r+') as fp:
    res=fp.read()
    print('高级语法:',res) # hello world python
    fp.write(res)

# a+ 追加写入
with open('./1.txt','a+') as fp:
    # 设置指针到文件起始位置
    fp.seek(0)
    res=fp.read()
    fp.write(res)

# x+ 异或,文件存在会报错
# with open('./2.txt','x+',encoding='utf-8') as fp1:
#     fp1.write('hello world')
#     fp1.seek(0)
#     res=fp1.read()
#     print('x+==',res) # x+== hello world

# 写入相关操作

# 写入相关函数
varstr='phton'
with open('./txt/1.txt','w',encoding='utf-8') as fp:
    fp.write(varstr)

# 容器类型数据写入文件
varlist=['java','python','c','c++']
with open('./txt/2.txt','w',encoding='utf-8') as fp:
    # 入容器类型数据时需要使用writelines方法,或varlist.__str__()转成字符串后写入
     fp.writelines(varlist) # 写 ['java', 'python', 'c', 'c++']

# 读取相关函数
with open('./txt/2.txt','r',encoding='utf-8') as fp:
    res=fp.read() # 默认从当前指针位置读取内容
    fp.seek(0) # 切换指针到起始位置
    resb=fp.read(3) # 设置读取的字符长度
print(res)  # javapythoncc++
print(resb) # jav

# 读取一行数据
# 添加3.txt,并填写内容
with open('./txt/3.txt','r',encoding='utf-8') as fp:
    res=fp.readline()
    print(res) # 床前明月光
    res=fp.readline()
    print(res) # 疑是地上霜
    res=fp.readline(3)
    print(res) # 举头望
    fp.seek(0)
    res=fp.readlines()
    print(res) # ['床前明月光\n', '疑是地上霜\n', '举头望明月\n', '低头思故乡']
    varlist=[i.strip("\n") for i in res]
    print(varlist)
    fp.seek(6) # 设置文件指针的起始位置,按照字节数计算
    print('==============')
    res=fp.read()
    print(res)

# truncate() 截取文件内容
with open('./txt/3.txt','r+',encoding='utf-8') as fp:
    # 从文件首行第一个字符开始阶段,长度为res
    # 如果为0,则从当前位置截取到最后
    res=fp.truncate(1)
    res=fp.truncate(0)
    print(res)
    print(res)
原文地址:https://www.cnblogs.com/hekaiqiao/p/15603345.html