Python实现简单的文件操作合集
作者:顾城沐心 时间:2022-03-25 05:00:35
一、文件操作
1.打开
r+ 打开存在文件 文件不存在 报错
file = open("user.txt","r+")
print(file,type(file))
w+ 若是文件不存在 会创建文件
file = open("user.txt","w+")
print(file,type(file))
2.关闭
file.close()
3.写入
file = open("user.txt","w+")
print(file,type(file))
file.write("hello\n")
file.close()
4.读取
print(file.readlines())
二:python中自动开启关闭资源
写入操作
stu = {'name':'lily','pwd':'123456'}
stu1 = {'name':'sam','pwd':'123123'}
#字典列表
stu_list = [stu,stu1]
#写入操作
with open("user.txt",mode='a+') as file:
for item in stu_list:
print(item)
file.write(item['name']+" "+item['pwd']+"\n")
读取操作
#读取操作
with open("user.txt",mode='r+') as file:
lines = file.readlines()
for line in lines:
line = line.strip() #字符串两端的空格去掉
print(line)
#读取操作
with open("user.txt",mode='r+') as file:
lines = file.readlines()
for line in lines:
#字符串分割 空格分割出用户名和密码
name , pwd = line.split(" ")
print(name,pwd)
user_list = []
#读取操作
with open("user.txt",mode='r+') as file:
lines = file.readlines()
for line in lines:
line = line.strip() #字符串两端空格去除 去除\n
name,pwd= line.split(" ") #用空格分割
user_list.append({'name':name,'pwd':pwd})
print(user_list)
user_list = []
#读取操作
with open("user.txt",mode='r+') as file:
lines = file.readlines()
for line in lines:
name,pwd = line.strip().split(" ")
user_list.append({'name':name,'pwd':pwd})
print(user_list)
读写函数简单封装
# 写入操作 封装
def write_file(filename,stu_list):
with open(filename,mode='a+') as file:
for item in stu_list:
file.write(item['name'] + " " + item['pwd'] + "\n")
#读取操作 函数封装
def read_file(filename):
user_list = []
with open(filename,mode='r+') as file:
lines = file.readlines()
for line in lines:
name,pwd = line.strip().split(" ")
user_list.append({'name':name,'pwd':pwd})
return user_list
来源:https://blog.csdn.net/m0_56051805/article/details/126982476
标签:Python,文件,操作
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
讲解如何利用 Python完成 Saga 分布式事务
2021-08-19 00:03:48
![](https://img.aspxhome.com/file/2023/3/117883_0s.jpg)
kafka监控获取指定topic的消息总量示例
2023-09-04 01:44:48
使用Python手工计算x的算数平方根,来自中国古人的数学智慧
2021-12-07 01:29:53
![](https://img.aspxhome.com/file/2023/6/107666_0s.png)
python之tensorflow手把手实例讲解斑马线识别实现
2021-11-11 05:53:19
![](https://img.aspxhome.com/file/2023/0/68460_0s.png)
python字典如何获取最大和最小value对应的key
2021-07-10 11:14:22
使用python爬取抖音视频列表信息
2023-04-06 12:51:48
![](https://img.aspxhome.com/file/2023/3/107283_0s.jpg)
实现UTF8转换GB2312国标码的asp代码
2011-02-28 10:53:00
浏览器中的内存泄露(续)解决方案
2008-05-03 17:14:00
Python实现word2Vec model过程解析
2023-10-07 14:22:10
![](https://img.aspxhome.com/file/2023/4/63254_0s.jpg)
python中如何使用insert函数
2023-08-02 17:04:43
Python hashlib常见摘要算法详解
2023-07-29 20:04:09
pandas 小数位数 精度的处理方法
2022-12-16 20:40:05
asp.net 防止用户通过后退按钮重复提交表单
2023-07-21 00:03:54
详解Python中的文本处理
2023-07-02 08:14:00
详解python 破解网站反爬虫的两种简单方法
2023-11-19 21:29:55
![](https://img.aspxhome.com/file/2023/2/109532_0s.jpg)
浏览器发送URL的编码特性
2007-10-12 20:51:00
asp如何读取Windows的信息文件(.ini)?
2009-11-20 18:27:00
python之如何查找多层嵌套字典的值
2021-12-05 08:57:07
如何用Python来理一理红楼梦里的那些关系
2023-03-28 08:56:31
ORACLE 10g 安装教程[图文]
2009-05-24 19:12:00
![](https://img.aspxhome.com/file/UploadPic/20095/26/200952620153777.jpg)