zoukankan      html  css  js  c++  java
  • python文件操作

    python的文件操作

     

    文件读写

    文件读的操作步骤

    • 打开文件
    • 操作文件描述 读/写
    • 关闭文件(易忘)

    文件打开

    file = open("文件路径以及文件名","打开模式"

    文件关闭

    file.close()
    ------------------
    ## 读
    
    ```python
    # 第一步,打开文件(以只读模式)
    file = open("book.txt","r")
    # 第二步,读取文件
    file.read()
    # 第三步,关闭文件
    file.close()

    遍历打开文件中的每一行

    with open("book.txt","r",encoding="UTF-8") as b
    for i in b.readlines():
        print(i)
    • read() :括号内可以写数字,表示的是读取多少个字符,默认读取所有
    • readline:一次读取文件的一行内容
    • readlines:一次读取文件的所有内容,一行内容为一个元素,以列表的形式展示

    文件的打开模式

    # 第一步,打开文件(以只写模式)
    file = open("book.txt","w")
    # 第二步,写入文件
    file.write("内容")
    # 第三步,关闭文件
    file.close()
    • 写入内容会依次追加到最后

    生成备份文件

    file_Name = input("请输入要备份的文件名")
    file_1 = open(file_Name,"r")
    # index 是找到file_name中‘.’的索引值
    index = file_Name.rfind('.')
    if index > 0:
        new_name = file_Name[:index]+"备份"+file_Name[index:]
    else:
        new_name = file_Name+"备份"
    
    
    file_2 = open(new_name,"w")
    count = file_1.read()
    file_2.write(count)
    
    file_1.close()
    file_2.close()

    获取文件当前的读写位置

    file.tell()

    文件下表从0开始,文件打开时,指针在0

    file = open("bbb.txt", encoding="utf-8")
    print(file.tell())
    content = file.read(15)
    print(content)
    print(file.tell())
    file.close()

    查看与修改文件的读写位置,seek(offset,from)

    • offest:偏移的字节数
    • from:从哪开始,三个选择
      0:从文件开始,默认
      1:从文件的当前位置开始,文件以非二进制的模式打开,只能跳转0
      2:文件末尾开始
    file = open(("aaa.txt"),"r+")
    print(file.tell())
    file.seek(5,0)
    print(file.tell())
    print(file.read(5))
    print(file.tell())
    print(file.seek(0,1))  # 从文件当前位置偏移0个字节
    print(file.seek(5,1))  # 从文件当前位置偏移5个字节
    file.close()

    删除指定的文件

    • 需要引入os模块
      格式: os.remove("文件名")
    import os
    os.remove("bbb.tst")

    pickle.dump方法和pickle.load方法的运用

    import pickle
    class Person:
        def __init__(self,name,age,sex,id):
            self.name = name
            self.age = age
            self.sex = sex
            self.id = id
    
        def learn(self):
            print("我喜欢学习Python语言")
    #
    #
    zs = Person("张三", 23, "", 12345678)
    ls = Person("里斯", 20, "", 1234567890)
    print(zs.age)
    
    dict_1 = {}
    
    dict_1[zs.id] = zs
    dict_1[ls.id] = ls
    print(dict_1[zs.id])
    # 这里输出的内存地址
    print(dict_1[ls.id])
    # 将字典保存至文本文件中(.txt)
    file = open("E:\project\rotect.txt", "wb") # 这里不支持encoding="utf-8"
    # write只能写入字符串内容,不能直接存字典
    pickle.dump(dict_1,file)
    file.close()
    #
    file = open("E:\project\rotect.txt", "rb") # 这里不支持encoding="utf-8"
    # pick.load  用于读出数据
    count = pickle.load(file)
    dict_2 = count
    print(type(dict_2), dict_2)
     
  • 相关阅读:
    【C语言学习】-08 指针
    【C语言学习】-07 结构体
    【C语言学习】-06 函数
    百度地图添加大头针和视图
    ios8中修改的 推送和地图
    ios 有关设备信息
    详解Objective-C runtime
    IOS7 ~ Xcode5 制作 framework
    IOS 取消表格单元格 TableViewCell 去掉高亮状态 点击Cell取消选择状态
    验证码设计(转)
  • 原文地址:https://www.cnblogs.com/weisimin123/p/13889441.html
Copyright © 2011-2022 走看看