zoukankan      html  css  js  c++  java
  • Python学习笔记:文件读/写方法汇总

    # ############
    # 文件操作方法
    # 重点常用方法标红

    # ############
    import time, sys

    # ########### 读文件 ###########################################
    # 注意:读文件模式不能写,写文件模式则不能读,要指定相应的打开文件的模式mode="?"
    file = open("file.txt", encoding="utf-8", mode="r")
    print(file.read())
    file.close()

    # 一行一行读,(只能用来读小文件,大的文件就很慢,因为这是一次性读到内存)
    file = open("file.txt", encoding="utf-8", mode="r")
    for idx, line in enumerate(file.readlines()):
    print("第%d行" % (idx + 1), line.strip())
    file.close()

    # 一行一行读,file变成一个迭代器,内存里面只保留一行(效率最高,推荐用这一种)
    file = open("file.txt", encoding="utf-8", mode="r")
    idx = 0
    for line in file:
    idx += 1
    print("第%d行" % (idx), line.strip())
    file.close()

    # 控制指针来回读取
    file = open("file.txt", encoding="utf-8", mode="r")
    file.read()
    print("显示指针的位置:", file.tell()) # 显示指针位置
    file.seek(0) # 让指针回到某个位置
    print("显示指针的位置:", file.tell()) # 显示指针位置
    file.read()

    # 读写,可以读的同时将文本写到文件末尾(不能写到中间),比较常用。
    # file = open("file.txt", encoding="utf-8", mode="r+")
    # 写读,新建文件后再写(没有使用场景,不使用)
    # file = open("file.txt", encoding="utf-8", mode="w+")
    # 追加读写(没有使用场景,不使用)
    # file = open("file.txt", encoding="utf-8", mode="a+")
    # 二进制读:就不需要设置编码格式了,一般是网络传输中使用二进制读
    # file = open("file.txt", mode="rb")
    # 二进制写:需要使用encode转成字节码,需要先指定之前的编码集utf-8然后python帮你转成字节码,写入文件的字符不是0101显示的而是以二进制编码的
    # file = open("file.txt", mode="wb", encoding="utf-8")
    # file.write("hello binary ".encode())



    # ########### 写文件 ###########################################
    # 覆盖写入
    file = open("file.txt", encoding="utf-8", mode="w")
    print(file.write("ni hao "))
    file.close()
    # 追加内容,添加在文件末尾
    file = open("file.txt", encoding="utf-8", mode="a")
    print(file.write(" hello world "))
    file.close()

    # 使用flush()立刻从内存缓存中写入到硬盘,这个方法可以用来做进度条
    for i in range(20):
    if i < 50:
    sys.stdout.write(">")
    else:
    sys.stdout.write(" Done 100%") # sys.stdout为标准输出对象
    time.sleep(0.1)
    sys.stdout.flush() # 这里用的不是file中的flush方法,但是可以说明问题,用flush的时候就是立刻将内存中的数据输出

    # 从头截断某个字符长度并且赋值给文件
    file = open("file.txt", encoding="utf-8", mode="a")
    file.truncate(10)


    # ########### 修改文件 ###########################################

    # 思路:打开一个新文件,将内容从旧文件读出,修改后写入新文件,
    # 硬盘的特殊结构决定,直接修改后面的字符就没了。所以只能用这种方式,
    file_old = open("file.txt", encoding="utf-8", mode="r")
    file_new = open("file_new.txt", encoding="utf-8", mode="w")

    # 取出来一行,然后修改后写到另外一个新文件中
    for line in file_old:
    if "1" in line:
    line = line.replace("1", "A")
    file_new.write(line)
    file_new.close()
    file_old.close()

    # 也可以像下面这样写,用with语句块就不需要close方法了,同时也可以打开多个文件,中间用逗号隔开
    with open("file.txt", encoding="utf-8", mode="r") as f1,
    open("file_new.txt", encoding="utf-8", mode="w") as f2:
    for line in f1:
    if "1" in line:
    line = line.replace("1", "b")
    f2.write(line)
  • 相关阅读:
    Scala Ant Tasks
    Git挂钩
    读写文件
    DC10用CSS定位控制网页布局
    table设置colspan属性,列宽显示错位解决方法
    ATM和购物商城-错题集
    python 函数参数多种传递方法
    python 函数 初学
    python 集合 gather
    元组 字体高亮 购物车练习
  • 原文地址:https://www.cnblogs.com/tangwei-fuzhou/p/12653914.html
Copyright © 2011-2022 走看看