Python学习 之 文件操作


创建文件句柄

'''
	1. open是操作系统的指令
	2. 文件路径: xx/xx/xx.txt, 可以使用绝对路径或者相对路径, 路径中出现反斜杠会报错, 推荐在引号前加r(全部转义)
	3. 打开编码方式: 跟随系统, windows默认编码是gbk, linux和mac默认编码是utf-8
	4. 动作: 读/写/追加, mode默认模式为r(只读模式)
	5. 文件句柄常用命名: f1 / file / f_handle / file_handle / f_obj

'''
# 方式 1, 推荐, 不需要手动关闭
with open("xx/xx.txt", encoding="utf-8", mode="r") as f:
	# do sth. # 操作


# 方式 2	
f1 = open('xx/xx/xx.txt', encoding='utf-8', mode='r')
# do sth.  # 操作文件句柄
f1.close() # 关闭文件句柄


操作模式

参数描述
r / rb只读
w / wb只写
a / ab追加
r+ / r+b先读后追加, 避免使用
w+ / w+b先写后读, 避免使用
a+ / a+b避免使用

  • r 模式 (只读)
    ''' 原文件: utf-8编码的"中国" '''
    
    with open("a.txt", "r", encoding="utf-8") as f:
    	print(f.read()) # 中国
    
    

  • rb 模式 (不做编码转化的只读, 一般用于操作非文字类文件, encoding参数可以不写)
    ''' 原文件: utf-8编码的"中国" '''
    
    with open("a.txt", "rb") as f:
    	print(f.read()) # b'\xe4\xb8\xad\xe5\x9b\xbd
    	
    

  • w 模式
    '''
    	1. 若文件不存在, 则创建文件, 并写入内容
    	2. 若文件存在, 则清空文件内容, 并写入内容
    
    '''
    
    with open("c.txt", encoding="utf-8", mode="w") as f:
        s = "写入内容"
        f.write(s)
    	
    

  • wb 模式
    ''' 写入 bytes 格式的文件, 常用于写入图片等非文本文件 '''
    
    with open("b.txt", mode="wb") as f:
        s = "写入内容"
        # 使用 utf-8 编码, 将 str 转为 bytes 类型
        content = s.encode("utf-8")
        # content = s.encode("gbk") # 使用 gbk 编码的 bytes 类型写入, 在默认解码为 utf-8 的时候会显示乱码
        f.write(content)
        
    

  • a 模式
    '''
    	1. 若文件不存在, 则创建文件, 并写入内容
    	2. 若文件存在, 则直接在文件末尾写入内容
    
    '''
    
    with open("c.txt", encoding="utf-8", mode="a") as f:
        s = "写入内容"
        f.write(s))
    
    

  • ab 模式
    ''' 追加 bytes 格式的文件 '''
    
    with open("c.txt", mode="ab") as f:
        s = "写入内容 2"
        # content = s.encode("utf-8")
        content = s.encode("gbk") # 使用 gbk 编码的 bytes 类型写入, 在默认解码为 utf-8 的时候会显示乱码
        f.write(content)
        
    

常用方法

读文件

  • for 循环 - 逐行读, 末尾自动退出
    ''' 读到末尾自动退出 '''
    
    with open("a.txt", "r", encoding="utf-8") as f:
    	for i in f:
    	      print(i) 
    	      
    

  • read() - 读全文
    """
    	不传参, 读取全文
    	传参, 如果是 r 模式, 最小元素为字符, 如果是 rb 模式, 最小元素为字节
    
    """
    
    with open("a.txt", "r", encoding="utf-8") as f:
        # 读全文
        print(f.read())
    
        # 按最小元素读取
        print(f.read(10))
    
    

  • readline() - 逐行读, 末尾持续返回空行
    ''' 一行一行读, 从第一行开始, 不会自动停止 '''
    
    with open("a.txt", "r", encoding="utf-8") as f:
    	print(f.readline())
    	print(f.readline()) # 末尾之后会返回空行
    	
    

  • readlines() - 不推荐使用, 可能导致内存溢出
    '''
    	读取全文, 每一行内容作为列表的一个元素, 可能导致内存溢出, 不推荐使用
    	readlines可以设定读取的最大字节大小 readlines(sizehint)
    
    '''
    
    with open("a.txt", "r", encoding="utf-8") as f:
    	print(f.readlines())
    	
    

写文件

  • write() - 全部写入
    """
    	全部文本写入
    	可写单行, 可写多行
    
    """
    
    s = """fajdifopajeipafeajfpoa
    wjeifopajefi
    ajfiep
    附件eipafeifoj
    """
    
    with open("b.txt", mode="w") as f:
        f.write(s)
        
    

  • writeline() - iterable 写入
    """ 将 iterable 依次写入 """
    
    # 创建一个可迭代对象
    s = [str(i) for i in range(30)]
    
    with open("b.txt", mode="w") as f:
        f.writelines(s)
        
    

  • flush() - 立即写入, 刷新缓冲区
    """
    	刷新缓冲区,即将缓冲区中的数据立刻写入文件,同时清空缓冲区,不需要是被动的等待输出缓冲区写入。
    	一般情况下,文件关闭后会自动刷新缓冲区,但有时你需要在关闭前刷新它,这时就可以使用该方法。
    
    """ 
    import sys, time
    
    for i in range(10):
        sys.stdout.write("*")
        sys.stdout.flush()
        time.sleep(0.2)
        
    

光标的使用

""" 文件内容: content """

with open("c.txt", mode="r") as f:
    # 光标后移 3 位, 换行符也算 1 位
    f.seek(3)
    # 获取光标当前位置
    f.tell()

    # 读取内容
    print(f.read())  # tent

    # 将光标移到最前
    f.seek(0, 0)

    print(f.read())  # content

    # 将光标移到末尾
    f.seek(0, 2)
    print(f.read())  # "", 空字符串


用于容错判断

# 是否可读
f.readable()

# 是否可写
f.writable()

# 光标是否可操作
f.seekable()


示例 - 修改文件

import os

''' 方法1, 用for语句逐行读取内容, 不会占用过大内存, 推荐使用 '''

# 1. 打开两个文件句柄
with open("xx.txt",encode="utf-8",mode="r") as f1, open("xx.bak",encode="utf-8",mode="w") as f2:
	# 2. for 循环遍历旧文件
    for i in f1:
    	# 3. 将旧文件中需要修改的地方替换
        new_content = i.replace("old_str", "new_str")
        # 4. 将替换好的文本写入新文件
        f2.write(new_content)
        
# 5. 删除旧文件
os.remove('xx.txt')

# 6. 将新文件命名为旧文件
os.rename('xx.bak', 'xx.txt')

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值