Python学习5——文件操作

转载 原文Python学习之路day3-文件操作https://www.cnblogs.com/linupython/p/6508121.html

1、python操作文件流程

# python操作文件流程
# test.txt文件路径为为相对路径,即E:\python\test.python
f=open("test.txt","r",encoding="utf-8")   # open创建句柄打开文件
content=f.read()                          # 通过句柄对文件进行操作
print(f.read())
f.close()                                 # 关闭文件

2、文件打开模式

# 普通打开模式
# r模式(只读模式)
# 只读模式下,写入会报错,not writable
f=open("test.txt","r",encoding="utf-8")
print(f.read())
f.write("Hello\n")
f.close()

# w模式(写模式)
# 读操作报错not readable
f=open("test.txt","w",encoding="utf-8")
f.write("Hello\n")
f.read()
f.close()

# 文件覆盖写与移动指针
# open文件之后第一次写是覆盖写。close前的第二次写却是追加写
f=open("test.txt","w",encoding="utf-8")
f.write("first write\n")
f.write("second write\n")
f.close()

# a模式(追加模式),不论指针
# w,指针
f=open("test.txt","w",encoding="utf-8")
f.write("3\n")
f.write("4\n")
f.seek(0)
f.write("5\n")
f.seek(20)
f.write("6\n")
f.close
# a模式,不论指针
f=open("test.txt","a",encoding="utf-8")
f.write("a\n")
f.seek(0)
f.write("b\n")
f.close()

# r+模式(读写模式)(open时还是覆盖写)
# 同时具备读写权限,追加写,除非把指针移到文件头
f=open("test.txt","r+",encoding="utf-8")
f.write("hh\n")           # open时为覆盖写
f.write("hh1\n")          # 顺序写
f.write("hh2\n")          # 顺序写
f.seek(0)
f.write("hh3\n")
f.close()

# w+模式
# 写读模式,同时具备写和读权限,先创建新的空文件,然后写入内容。该模式实际不常用。

# a+模式
# 追加内容的同时可读,注意新内容一定是在源文件末尾追加
# 同时在读取文件内容时文件指针默认就在文件末尾
# 因此不移动文件指针到文件头部是不能读取到文件内容的

# 常见的二进制打开模式下的操作有rb(读二进制),wb(写二进制)和ab(追加二进制)

3、文件常用函数及基本操作

# 文件读
# Somehow, it seems the love I knew was always the most destructive kind.
# Yesterday when I was young.
# read()
f=open("test.txt","r",encoding="utf-8")
# print(f.read())
print(f.read(2))              # 读取字符串长度
f.close()

# readline(),一次读取一行
f=open("test.txt","r",encoding="utf-8")
print(f.readline())
f.close()

# readlines(),列表形式读取全部内容
f=open("test.txt","r",encoding="utf-8")
print(f.readlines())
f.close()

# line in f逐行读取
f=open("test.txt","r",encoding="utf-8")
for line in f:
    print(f)
    print(type(f))
f.close()

# tell(),返回当前指针位置
f=open("test.txt","w",encoding="utf-8")
f.write("wow\n")
print(f.tell())
f.close()

# seek(),0表示文件头
f=open("test.txt","r",encoding="utf-8")
f.seek(0)
f.close()

# encoding,返回编码格式
f=open("test.txt","r",encoding="utf-8")
print(f.encoding)
f.close()

# fileno(),返回文件描述符
f=open("test.txt","r",encoding="utf-8")
print(f.fileno())
f.close()

# seekable,文件指针是否可移动
f=open("test.txt","r",encoding="utf-8")
print(f.seekable())
f.close()

# readable,文件是否可读
f=open("test.txt","r",encoding="utf-8")
print(f.readable())
f.close()

# writable,文件是否可写
f=open("test.txt","r",encoding="utf-8")
print(f.writable())
f.close()

# flush(),将缓存的数据同步刷写到磁盘

# truncate(),文件从文件头开始截取n给字符
# 起始位置总是文件头,seek跳转无效,文件具有写权限
f=open("test.txt","r+",encoding="utf-8")
f.truncate(5)
print(f.read())
f.close()

# close()
# 关闭打开的文件句柄,切记切记!
# closed
# 判断打开的文件句柄是否已关闭,返回布尔值。

4、文件编辑

# 文件内容编辑
# 将文件加载到内存,读取修改,类似vim,使用于小文件
# 读取源文件加以修改为新文件
for line in f:
    if "hh" in line:
        line=line.replace("hh","wow")
    f2.write(line)

5、with语句

# with,自动关闭文件句柄,判断条件应该是格式
# with open("test.txt", "r", encoding="utf-8") as f1,\
#         open("test2.txt", "r+", encoding="utf-8") as f2:
with open("test.txt","r",encoding="utf-8")as f:
    print(f.closed)               # 注意空格
    for line in f:
        print(line)
    print("你好")
    f.seek(0)
    print(f.closed)
    print(f.read(10))
print(f.closed)                   # 注意格式,此处顶格则为结束

 

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值