文件的常用操作

目录

一、文件的常用操作

1、创建文件

2、读文件

3、写文件

4、删除文件


一、文件的常用操作

1、创建文件

需求:在d盘的a目录下创建hi.txt文件,a目录已经创建好

# 需求:在d盘的a目录下创建hi.txt文件,a目录已经创建好
"""
    1.如果我们要创建一个文件,只需要以mode="w"形式打开文件即可
    2.如果该文件不存在,系统就会自动创建该文件
    3.注意:encoding="utf-8"是关键字参数,不能少encoding,
      因为encoding并不是open()方法的第三个形参
"""

f1=open("d://a//hi.txt","w",encoding="utf-8")
print(f"文件创建成功,类型是{type(f1)}")

2、读文件

# 需求:读取在d盘的a目录下的hello.txt文件,hello.txt已经存在

hello.txt文件

方式1 

# 需求:读取在d盘的a目录下的hello.txt文件,hello.txt已经存在

# 打开文件
f=open("d://a//hello.txt","r",encoding="utf-8")

# 读取方式1:read(),一次返回整个文件的内容
content=f.read()
# 关闭文件,释放文件占用的系统资源
f.close()
print(content)

方式2 

# 需求:读取在d盘的a目录下的hello.txt文件,hello.txt已经存在

# 打开文件
f=open("d://a//hello.txt","r",encoding="utf-8")

# 读取方式2:f.readline(),注意readline,字符串末尾保留换行符\n
line1=f.readline()
line2=f.readline()
print(f"第1行数据是:{line1}")
print(f"第2行数据是:{line2}")
f.close()

# 需求:读取在d盘的a目录下的hello.txt文件,hello.txt已经存在

# 打开文件
f=open("d://a//hello.txt","r",encoding="utf-8")

# 读取方式2:f.readline(),注意readline,字符串末尾保留换行符\n
# line1=f.readline()
# line2=f.readline()
# print(f"第1行数据是:{line1}")
# print(f"第2行数据是:{line2}")
# f.close()

# 循环读取整个文件,一行一行的读取
while True:
    line_content=f.readline()
    # 如果为"",表示文件读取完毕
    if line_content=="":
        break
    print(line_content,end="")
f.close()

方式3

# 需求:读取在d盘的a目录下的hello.txt文件,hello.txt已经存在

# 打开文件
f=open("d://a//hello.txt","r",encoding="utf-8")

# 读取方式3:f.readlines()列表形式读取文件中的所有行
lines=f.readlines()
print(f"lines的类型是:{type(lines)}") # list
print(f"lines的内容是:{lines}")
print("---------------------------")

for line in lines:
    print(line,end="")
f.close()

方式4

# 需求:读取在d盘的a目录下的hello.txt文件,hello.txt已经存在

# 打开文件
f=open("d://a//hello.txt","r",encoding="utf-8")

# 读取方式4:for line in f形式读取文件
for line in f:
    print(line,end="")
f.close()

3、写文件

# 需求

1)在d盘的a目录下创建abc.txt文件,并写入10句"hello,python"到文件

2)在abc.txt内容覆盖成新的内容10句,"hi,c++"

3)在abc.txt原有内容基础上追加10句,"你好!java"

"""
    1.mode="w",打开文件,如果文件不存在,会创建;
    如果文件已经存在,会先截断打开的文件,也就是清空文件内容
    2.如果我们希望以追加的方式写入,需要mode="a"
"""
# w模式打开文件,文件截断原来内容
# f=open("d://a//abc.txt","w",encoding="utf-8")

# a模式打开文件,是在原来的基础上追加内容
# f=open("d://a//abc.txt","a",encoding="utf-8")

# 1)在d盘的a目录下创建abc.txt文件,并写入10句"hello,python"到文件
# f=open("d://a//abc.txt","w",encoding="utf-8")
# i=1
# while i<=10:
#     f.write(f"hello,python\n")
#     i+=1
# f.close()

# 2)在abc.txt内容覆盖成新的内容10句,"hi,c++"
# f=open("d://a//abc.txt","w",encoding="utf-8")
# i=1
# while i<=10:
#     f.write(f"hi,c++\n")
#     i+=1
# f.close()

# 3)在abc.txt原有内容基础上追加10句,"你好!java",指定为a追加模式即可
f=open("d://a//abc.txt","a",encoding="utf-8")
i=1
while i<=10:
    f.write(f"你好!java\n")
    i+=1
f.close()

4、删除文件

# 需求

1)判断在d盘的a目录下是否有hi.txt文件,如果有则删除

2)如果没有,则提示"文件不存在"

# 引入os模块
import os

if os.path.exists("d://a//hi.txt"):
    os.remove("d://a//hi.txt")
    print("文件已删除")
else:
    print("文件不存在")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值