• Python 文件简单操作


    目录

    一:open(r+/w+)、close、write、readlines

    二:with open .. as

    三:读写文件 函数封装


    一:open(r+/w+)、close、write、readlines

    1 open打开文件

    r+ 打开已经存在文件

    对于文件不存在的情况,编辑器会给出报错

    1. # open打开文件 r+打开已经存在的文件
    2. file = open("user.txt", "r+")
    3. print(file, type(file))

     w+ 若是文件不存在 则会创建文件

    1. # open打开文件 w+文件存在打开/文件不存在则创建
    2. file = open("user.txt", "w+")
    3. print(file, type(file))

      user.txt创建

    2 close关闭文件 

    file.close()

    3 write写入文件

    1. # open打开 w+文件存在打开/文件不存在创建
    2. file = open("user.txt", "w+")
    3. print(file, type(file))
    4. # write写入
    5. file.write("hello\n")
    6. # close关闭
    7. file.close()

    w+文件存在打开、写入文件

    4 readlines读取文件

    print(file.readlines())

    二:with open .. as

    python中存在有 自动开启关闭资源   with open   ..  as

    入文件操作

    1. # 字典 定义
    2. stu = {'name': 'lily', 'pwd': '123456'}
    3. # 字典 定义
    4. stu1 = {'name': 'sam', 'pwd': '123123'}
    5. # 字典列表 定义
    6. stu_list = [stu, stu1]
    7. # 写入操作 a+追加写入
    8. with open("user.txt", mode='a+') as file:
    9. for item in stu_list: # 遍历字典列表
    10. print(item)
    11. # 写入文件:用户名、密码 每行一个数据:\n
    12. file.write(item['name'] + " " + item['pwd'] + "\n")

    取文件操作

    首先:字符串两端空格去掉

    1. # 字典 定义
    2. stu = {'name': 'lily', 'pwd': '123456'}
    3. # 字典 定义
    4. stu1 = {'name': 'sam', 'pwd': '123123'}
    5. # 字典列表 定义
    6. stu_list = [stu, stu1]
    7. # 读取操作 r+打开已经有的文件读取
    8. with open("user.txt", mode='r+') as file:
    9. lines = file.readlines() # 读取每一行
    10. for line in lines: # 遍历列表
    11. line = line.strip() # 字符串两端的空格去掉
    12. print(line)

    输出结果:可以正确读取user.txt内容

    lily 123456
    sam 123123

    其次:需要空格分割出用户名、密码,操作如下 

    1. # 字典 定义
    2. stu = {'name': 'lily', 'pwd': '123456'}
    3. # 字典 定义
    4. stu1 = {'name': 'sam', 'pwd': '123123'}
    5. # 字典列表 定义
    6. stu_list = [stu, stu1]
    7. # 读取操作
    8. with open("user.txt", mode='r+') as file:
    9. lines = file.readlines()
    10. for line in lines:
    11. # 字符串分割 空格分割出用户名和密码
    12. name, pwd = line.split(" ")
    13. print(name, pwd)

      

    然后:读取,字典数据类型的用户名、密码 

    1. # 字典列表
    2. user_list = []
    3. # 读取操作 r+打开已经存在的文件读取
    4. with open("user.txt", mode='r+') as file:
    5. lines = file.readlines() # 读取每一行
    6. for line in lines:
    7. line = line.strip() # 字符串两端空格去除 去除\n
    8. name, pwd = line.split(" ") # 用空格分割
    9. user_list.append({'name': name, 'pwd': pwd}) # 字典列表
    10. print(user_list)

    输出结果:读取到的为字典,多用户--字典列表

    [{'name': 'lily', 'pwd': '123456'}, {'name': 'sam', 'pwd': '123123'}]

    最后:简单优化一下,如下 

    1. # 字典列表
    2. user_list = []
    3. # 读取操作
    4. with open("user.txt", mode='r+') as file:
    5. lines = file.readlines() # 读取每一行
    6. for line in lines:
    7. # 两端空格去除、空格分割
    8. name, pwd = line.strip().split(" ")
    9. # 字典列表
    10. user_list.append({'name': name, 'pwd': pwd})
    11. print(user_list)

    输出结果:字典列表

    [{'name': 'lily', 'pwd': '123456'}, {'name': 'sam', 'pwd': '123123'}]

    三:读写文件 函数封装

    写入文件 函数封装

    1. # 写入文件操作 函数封装
    2. def write_file(filename, stu_list):
    3. with open(filename, mode='a+') as file:
    4. for item in stu_list:
    5. file.write(item['name'] + " " + item['pwd'] + "\n")

    读取文件 函数封装

    1. # 读取文件操作 函数封装
    2. def read_file(filename):
    3. user_list = []
    4. with open(filename, mode='r+') as file:
    5. lines = file.readlines()
    6. for line in lines:
    7. name, pwd = line.strip().split(" ")
    8. user_list.append({'name': name, 'pwd': pwd})
    9. return user_list

    仅提供思路,仍需完善..... 

  • 相关阅读:
    手把手教你从安装CentOS7.4镜像开始,搭建IoT视频监控系统
    在 Next.js App目录中使用 Code Hike
    【Java面试】List接口
    精分合并抑郁康复经历分享:如何从死亡边缘回到生的海洋?
    torch函数学习
    会话跟踪技术
    目前放疗中可用的一些开源软件
    ModuleNotFoundError: No module named ‘lavis‘解决方案
    JavaScript的函数
    观察者模式有人会这个不,看来看去不会,第一次学这个看书也看不知道
  • 原文地址:https://blog.csdn.net/m0_56051805/article/details/126982476