Python open 读和写
Python open
2023-09-11 14:21:25 时间
# -*- coding: utf-8 -*-
# 测试文件名为:
# text.txt
# 测试文件内容为:
# abcdefg
# 每次操作后将文件复原
# r
# 以只读方式打开文件,文件不可写
# 要打开的文件不存在时会报错
# 文件的指针将会放在文件的开头
# 这是默认模式
# # file = open('test.txt', 'r')
# # FileNotFoundError: [Errno 2] No such file or directory: 'test.txt'
# file = open('text.txt', 'r')
# print(file.read())
# # abcdefg
# file.write('aaa')
# # io.UnsupportedOperation: not writable
# file.close()
# rb
# 以二进制格式打开一个文件用于只读,文件不可写
# 要打开的文件不存在时会报错
# 文件指针将会放在文件的开头
# 这是默认模式
# # file = open('test.txt', 'rb')
# # FileNotFoundError: [Errno 2] No such file or directory: 'test.txt'
# file = open('text.txt','rb')
# print(file.read())
# b'abcdefg'
# # file.write(b'aaa')
# # io.UnsupportedOperation: not writable
# file.close()
# r+
# 打开一个文件用于读写,写入内容为str
# 文件指针将会放在文件的开头
# 重新写入的内容从头开始替换
# file = open('text.txt', 'r+')
# file.write('aaa')
# file.close()
# file = open('text.txt','r')
# print(file.read())
# # 'abcdefg'
# file.close()
# rb+
# 以二进制格式打开一个文件用于读写,写入内容为bytes
# 文件指针将会放在文件的开头
# 重新写入的内容从头开始替换
# file = open('text.txt','rb+')
# # file.write('aaa')
# # TypeError: a bytes-like object is required, not 'str'
# file.write(b'aaa')
# file.close()
# file = open('text.txt','rb')
# print(file.read())
# # b'aaadefg'
# file.close()
# w
# 打开一个文件只用于写入,写入内容为str
# 文件不可读
# 如果该文件已存在则将其覆盖,原文件内容将清空
# 如果该文件不存在,创建新文件
# file = open('test.txt', 'w')
# 创建一个空文件
# file = open('text.txt', 'w')
# file.write('gfedcba')
# file = open('text.txt', 'r')
# print(file.read())
# file.close()
# wb
# 以二进制格式打开一个文件只用于写入,写入内容为bytes
# 文件不可读
# 如果该文件已存在则将其覆盖,原文件内容将清空
# 如果该文件不存在,创建新文件
# file = open('test.txt', 'wb')
# 创建一个空文件
# file = open('text.txt', 'wb')
# file.write(b'gfedcba')
# file = open('text.txt', 'r')
# print(file.read())
# file.close()
# w+
# 打开一个文件用于读写,写入内容为str
# 如果该文件已存在则将其覆盖,原文件内容将清空
# 如果该文件不存在,创建新文件
# file = open('test.txt', 'w+')
# 创建一个空文件
# file = open('text.txt', 'w+')
# file.write('gfedcba')
# file = open('text.txt', 'r')
# print(file.read())
# file.close()
# wb+
# 以二进制格式打开一个文件用于读写,写入内容为bytes
# 如果该文件已存在则将其覆盖
# 如果该文件不存在,创建新文件
# file = open('text.txt', 'wb+')
# file.write(b'gfedcba')
# file = open('text.txt', 'r')
# print(file.read())
# file.close()
# a
# 打开一个文件用于追加(只写),写入内容为str
# 如果该文件已存在,文件指针将会放在文件的结尾,新的内容将会被写入到已有内容之后
# 如果该文件不存在,创建新文件进行写入
# file = open('test.txt', 'a')
# 创建一个空文件
# file = open('text.txt', 'a')
# file.write('aaa')
# file.close()
# file = open('text.txt')
# print(file.read())
# file.close()
# ab
# 以二进制格式打开一个文件用于追加(只写),写入内容为bytes
# 如果该文件已存在,文件指针将会放在文件的结尾,新的内容将会被写入到已有内容之后
# 如果该文件不存在,创建新文件进行写入
# file = open('test.txt', 'ab')
# 创建一个空文件
# file = open('text.txt', 'ab')
# file.write(b'aaa')
# file.close()
# file = open('text.txt')
# print(file.read())
# file.close()
# a+
# 打开一个文件用于追加(读写),写入内容为str
# 如果该文件已存在,文件指针将会放在文件的结尾,新的内容将会被写入到已有内容之后
# 如果该文件不存在,创建新文件用于读写
# file = open('test.txt', 'a+')
# 创建一个空文件
# file = open('text.txt', 'a+')
# file.write('aaa')
# file.close()
# file = open('text.txt')
# print(file.read())
# file.close()
# ab+
# 以二进制格式打开一个文件用于追加(读写),写入内容为bytes
# 如果该文件已存在,文件指针将会放在文件的结尾,新的内容将会被写入到已有内容之后
# 如果该文件不存在,创建新文件用于读写
# file = open('text.txt', 'ab+')
# file.write(b'aaa')
# file.close()
# file = open('text.txt')
# print(file.read())
# file.close()
相关文章
- python 支付宝SDK
- python 3 open() default encoding
- 地球引擎初级教程——Python API 语法(内涵JavaScript转python工具包介绍)
- Google Earth Engine(python)——用 Python 修复影像栅格的接缝
- 【零基础学python】:清华官方出品的《看漫画学Python》全彩PDF,495页资源分享
- 如何快速学习python,学好python?能通过Python赚到的第一笔钱,有哪些经验可以分享吗?
- python为什么叫爬虫?python爬虫定义
- 每天一个python小知识——如何在Python 3中转换数据类型
- Python分享:python爬虫可以用来做什么?
- 总结的太到位:python 多线程系列详解
- Python win32api提取exe图标icon
- Python 插件安装
- Python 相对路径和绝对路径--python实战(九)
- 《精通Python网络爬虫:核心技术、框架与项目实战》——导读
- 《Python高手之路(第3版)》——1.3 版本编号
- 《“笨办法”学Python(第3版)》——习题1 第一个程序
- 《NLTK基础教程——用NLTK和Python库构建机器学习应用》——2.11 小结
- 《树莓派Python编程入门与实战》——2.4 总结
- python学习之OpenCV-Python模块的部分应用示例(生成素描图和动漫图)
- (数据科学学习手札71)利用Python绘制词云图
- python 中 open与with open 的区别
- 【转载】python的魔法方法———A Guide to Python's Magic Methods
- 华为OD机试 - 运动会(Python) | 机试题+算法思路+考点+代码解析 【2023】
- 【零基础高效率Python】第七天 异常处理
- Python 常见问题 之 python 安装包下载安装速度慢 的 快速解决方法(之一)
- Python 工具 之 Anaconda 的下载、安装、环境创建,以及结合Pycharm在其Python环境下运行第一个HelloWorld
- Python 工具 之 Windows 上 python 虚拟环境的搭建与简单使用的相关说明
- Python yield的用法
- GET RESTful With Python
- [Python]python+yaml三行代码完成配置文件灵活读取与warning:YAMLLoadWarning处理
- Selenium4 Python实现Page Factory设计模式,python新的定位方法
- Python 颜色检测