zoukankan      html  css  js  c++  java
  • Python入门文件处理

    文件处理
    文本模式
    name = input("请输入用户名:").strip()
    with open("a.txt","wt",encoding="utf-8") as f:
    f.write(name)

    with open("a.txt","r",encoding="utf-8") as f:
    data = f.read()

    name = input("请输入用户名:").strip()
    with open("a.txt","wt",encoding="utf-8") as f:
    f.write(data)
    f.write(" ")
    f.write(name+" ")

    name = input("name:").strip()
    with open("a.txt",mode="at",encoding="utf-8") as f:
    f.write(name+" ")

    字节模式
      b表示字节模式
      与文本相同在于都必须与r、w、a连用
      rb 只读字节模式
      wb 只写字节模式
      ab 追加只写字节模式
      b模式可以操作任意类型的文件
      注意:
        1.b模式不允许设置编码参数
        2.读写数据都以字节为单位
    with open("a.txt",mode="rb")as f:
    data = f.read()
    print(data)
    print(type(data))
    print(data.decode("utf-8"))

    with open("2.文本模式回顾.mp4",mode="rb")as f:
    data = f.read(1024*1024)
    print(data)
    print(type(data))
    # print(data.decode("")) #

    with open("a.txt", mode="wb")as f:
    f.write("123".encode("utf-8"))
    print(type("123".encode("utf-8")))

    with open("a.txt", mode="ab")as f:
    f.write("123".encode("utf-8"))
    print(type("123".encode("utf-8")))

    可读可写模式
      +表示可读可写模式
      也不能单独使用
        r+ == r+t 直接写入会把原数据覆盖掉
        w+ == w+t 打开就清空所以读不到
        a+ == a+t 打开就移动光标到末尾也读不到
      可读可写字节模式
        r+b 直接写入会把原数据覆盖掉
        w+b 打开就清空所以读不到
        a+b 打开就移动光标到末尾也读不到
      每一个都有问题所以可读可写并不常用
    with open("a.txt",mode="r+",encoding="utf-8") as f:
    # print(f.read())
    f.write("abc")
    复制文件
    复制文件?
      从源文件读取写入到新文件中
      打开源文件
    source_f = open("a.txt","rb")
      打开目标文件
    dst_f = open("a副本.txt","wb")
      从源文件读取数据写入到目标文件
    data = source_f.read()
    dst_f.write(data)

    source_f.close()
    dst_f.close()
      并列打开多个文件可以用逗号隔开
    with open("a.txt","rb") as source_f,open("a副本.txt","wb") as dst_f:
    data = source_f.read()
    dst_f.write(data)
      with 嵌套语法
    with open("a.txt","rb") as source_f:
    with open("a副本.txt","wb") as dst_f:
    data = source_f.read()
    dst_f.write(data)
      with的嵌套时注意:不要对同一个文件进行操作
    with open("l.txt","rb") as source_f:
    with open("l.txt","wb") as dst_f:
    data = source_f.read()
    dst_f.write(data)
      让用户指定要复制的文件
    source_path = input("请输入源文件路径:").strip()
    dst_path = input("请输入目标文件路径:").strip()

    with open(source_path,"rb") as source_f:
    with open(dst_path,"wb") as dst_f:
    data = source_f.read()
    dst_f.write(data)
      基于CMD的复制工具要达到的效果在CMD中直接输入要复制的文件路径以及目标文件路径一回车就完成复制
        1.需要获取CMD输入的参数
    import sys # 有一个文件叫sys import是导入这个文件
    sys文件里有一个变量名称叫argv
    print(sys.argv) # 得到的就是执行解释器时传入的参数,第一个参数默认就是当前执行文件
        源文件路径
    source_path = sys.argv[1]
        目标文件路径
    dst_path = sys.argv[2]

    with open(source_path,"rb") as source_f:
    with open(dst_path,"wb") as dst_f:
    data = source_f.read()
    dst_f.write(data)
        文件对象其他操作
    f = open("userdb.txt","at",encoding="utf-8")
    for i in range(10):
    data = input(">>:").strip()
    f.write(data)
    print("写了一次")
    f.flush() # 立即将数据写入硬盘,可以保证数据不丢失,但是效率会降低
    f.close()

    print(f.closed) # 文件是否已经关闭
    print(f.encoding) # 获取文件的编码方式
    print(f.buffer) # 获取缓冲区 忘记它吧

    文件的修改
      1.读取文件数据到内存
      2.进行修改
      3.将修改后的数据写回文件中
    文本 userdb.txt
    admin|root|123
    jack|123|110
    aaaaa|123|110
    admin|123|123
    杰克逊|123123|123


      方法1:
    new_data = ""
    with open("userdb.txt",encoding="utf-8") as f:
    data = f.read()
    datas = data.split(" ")
    for line in datas:
    print(line.split("|")[0])
    name = line.split("|")[0]
    if name == "admin": # 取出名字判断是否等于admin
    name = name+"[is good man]" # 如果是则添加 is good man
    # 取出密码 和手机号 再次拼接为原来的样式
    pwd = line.split("|")[1]
    phone = line.split("|")[2]
    new_line = "|".join([name,pwd,phone])
    new_line += " "
    # 拼接修改过的数据
    new_data += new_line
    else:
    # 拼接未被修改的数据
    line += " "
    new_data += line

    print(new_data)
    with open("userdb.txt",mode="wt",encoding="utf-8") as f:
    f.write(new_data)
    以上代码存在什么问题相当于把整个文件数据都存到内存中会造成内存溢出
      方法2:
    读取一行修改一行修改完立马写入硬盘,避免了内存溢出的问题
    具体使用哪种方式的看文件大小
    较小的文件推荐一次性读到内存进行修改减少io操作
    较大的文件则必须采用第二种方式
    with open("userdb.txt",mode="rt",encoding="utf-8") as source_f,open("temp.swap",mode="wt",encoding="utf-8") as dst_f:
    for line in source_f:
    new_line = line.replace("admin","ADMIN")
    dst_f.write(new_line)

    import os
    os.remove("userdb.txt") # 删除源文件
    os.rename("temp.swap","userdb.txt") # 将交换文件重命名为源文件名

  • 相关阅读:
    animation动画应用--android游戏开发
    Codeforces Round #203 (Div. 2)
    Codeforces Round #206 (Div. 2)
    Codeforces Round #204 (Div. 2): C
    Codeforces Round #204 (Div. 2): B
    Codeforces Round #204 (Div. 2): A
    Codeforces Round #205 (Div. 2) : D
    Codeforces Round #205 (Div. 2) : C
    Codeforces Round #205 (Div. 2) : B
    Codeforces Round #205 (Div. 2) : A
  • 原文地址:https://www.cnblogs.com/ShenJunHui6/p/10239443.html
Copyright © 2011-2022 走看看