python中的文件IO

一. 数据的读写

1. 程序中的数据,写入到文件中

file = open('./data/1.1.text', mode='w', encoding='utf-8')
# 程序中有一个字符串
message = "hello,世界"
# 将数据写入到文件中
file.write(message)
# 关闭文件
file.close()

2. 将文件中的数据,读写到程序中

# 按照只读的方式打开文件
file = open(file='./data/1.1.text', mode='r', encoding='utf-8')
# 从文件中读取数据,展示到控制台中
info = file.read()
print(info)
# 关闭文件
file.close()

3. 文本文件的追加

file = open(file='./data/1.2.text', mode='a', encoding='utf-8')
# 要操作的文本数据
message = '人说,林深时见鹿,海蓝时见鲸,夜深时见你'
file.write(message)

message2 = '\n但是人说,林深时雾气,海蓝时浪涌,夜深时梦续'
file.write(message2)

message3 = '\r\n你可知:鹿踏雾而来,鲸随浪而起,你未曾转身,怎知我已到来...'
file.write(message3)
file.close()

4. 二进制文件的操作

# 读取计算机中的二进制文件数据
file = open(file='F:/images/001.jpg', mode='rb')
# 双引号字符串前面有个字母b表示是二进制数据、字母u表示Unicode数据,\x开头的是十六进制数据、\o开头的是八进制数据
# print(file.read())
# 将数据重新存储到指定的位置
file2 = open(file='./data/test.jpg', mode='wb')
file2.write(file.read())
# 关闭文件
file2.close()
file.close()

5. 文件的快捷操作:with语法

with open('F:/images/001.jpg', mode='rb') as file1:
    # 打开文件,将文件对象赋值给变量file1,with中的代码执行完成,文件file1自动关闭
    with open('./data/' + file1.name[file1.name.find('0'):], 'wb') as file2:
        # 将读取到的文件存储到指定的文件夹中
        file2.write(file1.read())

二. 文件的存取

1. 将程序中的字典数据,转换成字符串存储到文件中

users = {'admin': {'username': 'admin', 'password': '123', 'nickname': '小小'}}
users_str = str(users)
# 存储到文件中
with open('./data/2.1.text', 'w') as file:
    file.write(users_str)

2. 将文件中的字符串数据,读取到程序中

with open('./data/2.1.text', 'r') as file:
    users = file.read()
    print(users, type(users))  # {'admin': {'username': 'admin', 'password': '123'}} <class 'str'>
    # 将字符串数据,转换成字典:该字符串的格式~和python中的字典的表达式一致
    users = eval(users)
    print(users, type(users))  # {'admin': {'username': 'admin', 'password': '123'}} <class 'dict'>
    print(users.get('admin')) # {'username': 'admin', 'password': '123'}

3. 对数据进行无序化操作(json)

import json
# 准备操作的数据
users = {'admin': {'username': 'admin', 'password': '123'}}
users1 = {'admin': {'username': 'admin', 'password': '123'}}
# 1. 将程序中的数据,直接存储到文件中
with open('./data/3.1.json', 'w') as file:
    json.dump(users, file)
# 2. 将文件中的数据,读取到程序中
with open('./data/3.1.json', 'r') as file:
    users = json.load(file)
    print(users, type(users)) # {'admin': {'username': 'admin', 'password': '123'}} <class 'dict'>

4. 将程序中的多个数据存储到程序中(marshal)

# 数据准备
s = "字符串"
i = 19
f = 3.1415
b = True
c = 12 + 5j
l = [1, 2, 3]
d = {'username': 'admin', 'password': '123'}
x = [s, i, f, b, c, l, d]

import marshal
# 1. 存储多个数据到文件中
with open('./data/4.1.dat', 'wb') as file:
    # 第一次存储一个数量:有多少个数据存储到了文件中
    marshal.dump(len(x), file)
    # 存储每个数据
    for x1 in x:
        marshal.dump(x1, file)
# 2. 将多个数据从文件中依次取出
with open('./data/4.1.dat', 'rb') as file:
    n = marshal.load(file)
    # 将所有数据依次取出
    for x in range(n):
        print(marshal.load(file))

5. 存储多个数据到文件中(shelve)

# 数据准备
users = {'admin': {'username': 'admin', 'password': '123'}}
articles = {'标题': {'title': '标题', 'content': '文章内容', 'author': users.get('admin')}}

import shelve
file = shelve.open('./data/5.1')
# 1. 将多个数据按照key:value的形式存储到文件中
file['users'] = users
file['articles'] = articles
# 2. 从文件中根据key读取数据
print(file['users'], type(file['users']))

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值