Python open 读和写

简介: # -*- coding: utf-8 -*-# 测试文件名为:# text.txt# 测试文件内容为:# abcdefg# 每次操作后将文件复原# r# 以只读方式打开文件,文件不可写# 要打开的文件不存在时会报错# 文件的指针将会放在文件的开头# 这是默认模式# # file = open('test.
# -*- 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 open 关于读、写、追加的总结

知识在于点滴积累
目录
相关文章
|
7月前
|
安全 Python
Python中文件操作的详细使用:open()、os.open()和with open()
Python中文件操作的详细使用:open()、os.open()和with open()
85 0
|
1月前
|
安全 Python
解释一下Python中with open()语句的工作原理。
【2月更文挑战第10天】【2月更文挑战第28篇】解释一下Python中with open()语句的工作原理。
|
3月前
|
Python
python中 open() 和 File()
在Python中,open()是内置函数,而File是类。它们的区别和理解如下: 1. open()函数:open()函数用于打开一个文件,并返回一个文件对象。它有以下几个参数:
32 2
|
8月前
|
Python
Python 文件操作 with open()
Python 文件操作 with open()
53 0
|
9月前
|
JSON 数据格式 Python
Python中对open读取文件内容时的mode模式解析
Python中对open读取文件内容时的mode模式解析
94 0
|
10月前
|
Python
25.从入门到精通:Python3 File方法 open() 方法 file 对象
25.从入门到精通:Python3 File方法 open() 方法 file 对象
|
10月前
|
NoSQL Redis Python
python | 使用open读写文件
python | 使用open读写文件
126 0
|
11月前
|
数据库连接 数据处理 Python
翻开Python的宝匣:深度解析open()函数,解锁更多操作技巧。
翻开Python的宝匣:深度解析open()函数,解锁更多操作技巧。
|
Docker Python 容器
解决docker 2022-11-16 14:54:26 python: can‘t open file ‘/src/main.py‘: [Errno 2]
解决docker 2022-11-16 14:54:26 python: can‘t open file ‘/src/main.py‘: [Errno 2]
127 0
|
数据采集 存储 缓存
python open函数文本操作详解
python open函数文本操作详解
python open函数文本操作详解