python open 关于读、写、追加的总结
# -*- 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 关于读、写、追加的总结的更多相关文章
- 总结day7 ---- 文件操作,读,写,追加,以及相关方法
内容大纲 一:文件的基本操作, >常见问题 >encoding >绝对路径和相对路径的 二:文件的读写追加相关操作 >读(r, r+ ,rb,r+b) >写(w,w+,w ...
- Python小实验——读&写Excel文件内容
安装xlrd模块和xlwt模块 读取Excel文件了内容需要额外的模块-- \(xlrd\),在官网上可以找到下载:https://pypi.python.org/pypi/xlrd#download ...
- openpyxl -用于读/写Excel 2010 XLSX/XLSM文件的python库
openpyxl -用于读/写Excel 2010 XLSX/XLSM文件的python库¶ https://www.osgeo.cn/openpyxl/index.html
- Python调用OpenCV读显写
OpenCV提供了python的接口,而且很重要的一点是python下的很多接口名与C++的接口名是一样的,这一篇先记录python调用OpenCV去读取图像.显示图像和保存图像. 1.OpenCV读 ...
- java读/写文件
读取文件参考:https://blog.csdn.net/weixin_42129373/article/details/82154471 写入文件参考:https://blog.csdn.net/B ...
- Pandas 基础(4) - 读/写 Excel 和 CSV 文件
这一节将分别介绍读/写 Excel 和 CSV 文件的各种方式: - 读入 CSV 文件 首先是准备一个 csv 文件, 这里我用的是 stock_data.csv, 文件我已上传, 大家可以直接下载 ...
- Python3.x:python: extend (扩展) 与 append (追加) 的区别
Python3.x:python: extend (扩展) 与 append (追加) 的区别 1,区别: append() 方法向列表的尾部添加一个新的元素.只接受一个参数: extend()方法只 ...
- MyISAM Key Buffer 读/写/利用率(%) MylSAM平均每秒Key Buffer利用率(%) MylSAM平均每秒Key Buffer读命中率(%) MylSAM平均每秒Key Buffer写命中率(%)
MyISAM Key Buffer 读/写/利用率(%) MylSAM平均每秒Key Buffer利用率(%)MylSAM平均每秒Key Buffer读命中率(%)MylSAM平均每秒Key Buff ...
- Windows操作系统中的I/O(读/写 输入/输出)
导言 写一个Windows平台下的应用程序大多时候都是离不开读写文件,网络通信的. 比如一个服务应用程序来说,它可能从网络适配器接受用户的请求,对请求进行处理计算,最终将用户端所需的数据返回,中间可能 ...
- python 在图像上写中文字体 (python write Chinese in image)
本人处理图像的时候经常使用opencv的包,但是 cv2.putText 显示不了中文,所以查找了如何在python在图像上写中文的方法,在伟大的Stack Overflow上面找到一个方法,分享给大 ...
随机推荐
- [转]WordPress 主题教程 #2:模板文件和模板
本文转自:http://blog.wpjam.com/m/wp-theme-lesson-2-template-files-and-templates/ 模板文件(template files)和模板 ...
- MVC应用程序显示上传的图片(续)
上一篇<MVC应用程序显示上传的图片>http://www.cnblogs.com/insus/p/3597543.html 最后有提及没有实现用户点击图片,显示原图的功能.此篇Insus ...
- glob 在webpack中的使用。
glob 在webpack中对文件的路径处理非常之方便,比如当搭建多页面应用时就可以使用glob对页面需要打包文件的路径进行很好的处理. 官方文档地址 : https://www.npmjs.com/ ...
- EWS 流通知订阅邮件
摘要 查找一些关于流通知订阅邮件的资料,这里整理一下. 核心代码块 using System; using System.Collections.Generic; using System.Linq; ...
- Linux创建用户等操作
转自: https://www.linuxidc.com/Linux/2017-06/144916.htm 与大家分享下Linux系统中创建用户.设置密码.修改用户.删除用户的命令,希望对你有所帮助. ...
- Evolution(矩阵快速幂)zoj2853
Evolution Time Limit: 5 Seconds Memory Limit: 32768 KB Description Evolution is a long, long pr ...
- js文字滚动效果
function (global) { var logo = document.getElementById('logo'); var text = document.createTextNode(' ...
- npm WARN checkPermissions Missing write access to 解决办法
解决办法 删除掉 C:\Users\dd\AppData\Roaming\npm-cache\ C:\Users\dd\AppData\Roaming\npm\ 两个文件夹
- 04-基本的mysql语句
[转]04-基本的mysql语句 本节课先对mysql的基本语法初体验. 操作文件夹(库) 增 create database db1 charset utf8; 查 # 查看当前创建的数据库 sho ...
- CentOS7查看开放端口命令及开放端口号
CentOS 7查看以开放端口命令:firewall-cmd —list-ports 查看端口是否开放命令:第一个方法就是使用lsof -i:端口号命令行,例如lsof -i:80.如果没有任何信息输 ...