File operations 1
1:只读(‘r' 和 ’rb'以字节读)
f = open('d:\模特主妇护士班主任.txt',mode='r',encoding='UTF-8')
content = f.read()
print(content)
f.close()
相对路径
f = open('模特主妇护士班主任',mode='r',encoding='utf-8')
content = f.read()
f.close()
若以rb模式,则不需encoding=.......
2:读写('r+'和‘r+b'以bytes字节读写)
f = open('log',mode='r+',encoding='utf-8')
print(f.read())
f.write('大猛,小孟')
f.close()
f = open('log',mode='r+b')
print(f.read())
f.write('大猛,小孟'.encode('utf-8'))
f.close()
3:只写('w'和'wb')
先将源文件的内容全部清除,再写。
f = open('log',mode='w',encoding='utf-8')
f.write('附近看到类似纠纷')
f.close()
f = open('log',mode='wb')
f.write('附近看到类似纠纷'.encode('utf-8'))
f.close()
4:写读
w+ , w+b
f = open('log',mode='w+',encoding='utf-8')
f.write('aaa')
f.seek(0)
print(f.read())
f.close()
5:追加 'a'
f = open('log',mode='a',encoding='utf-8')
f.write('佳琪')
f.close()
f = open('log',mode='ab')
f.write('佳琪'.encode('utf-8'))
f.close()
注意:seek()--以字符定位光标位置,一个中文
字体代表3个字符,一个英文字母代表一个字符
6:文件操作其他功能
# obj = open('log',mode='r+',encoding='utf-8')
# content = f.read(3) # 读出来的都是字符
# f.seek(3) # 是按照字节定光标的位置
# f.tell() 告诉你光标的位置
# print(f.tell())
# content = f.read()
# print(content)
# f.tell()
# f.readable() # 是否刻度
# line = f.readline() # 一行一行的读
# line = f.readlines() # 每一行当成列表中的一个元素,添加到list中
# f.truncate(4)
# for line in f:
# print(line)
# f.close()
# f = open('log',mode='a+',encoding='utf-8')
# f.write('佳琪')
# count = f.tell()
# f.seek(count-9)
# print(f.read(2))
# f.close()
# with open('log',mode='r+',encoding='utf-8') as f,\
# open('log',mode='w+',encoding='utf-8') as f1:
File operations 1的更多相关文章
- python file operations
原文地址 总是记不住API.昨晚写的时候用到了这些,但是没记住,于是就索性整理一下吧: python中对文件.文件夹(文件操作函数)的操作需要涉及到os模块和shutil模块. 得到当前工作目录,即当 ...
- File Operations
在刷题测试程序时,为了避免每次都手工输入,我们可以把输入数据保存在文件中:为了避免输出太长,我们将输出也写入文件中,方便与标准答案文件进行比较. 文件使用一般有两种方法:输入输出重定向.fopen. ...
- backup, file manipulation operations (such as ALTER DATABASE ADD FILE) and encryption changes on a database must be serialized.
昨天在检查YourSQLDba备份时,发现有台数据库做备份时出现了下面错误信息,如下所示: <Exec> <ctx>yMaint.ShrinkLog</ctx> ...
- VMWare File Format Learning && Use VHD File To Boot VMWare && CoreOS Docker Configuration And Running
目录 . Virtual Machine Introduce . Vmware Image File Format . VHD File Format . Convert VHD File Into ...
- Python File I/O
File is a named location on disk to store related information. It is used to permanently store data ...
- File Operation using SHFileOperation
SHFILEOPSTRUCT Original link: http://winapi.freetechsecrets.com/win32/WIN32SHFILEOPSTRUCT.htm Refere ...
- python文件和文件夹訪问File and Directory Access
http://blog.csdn.net/pipisorry/article/details/47907589 os.path - Common pathname manipulations 都是和路 ...
- Java-Io之文件File
File是"文件"和"目录路径名"的抽象表示形式.File之间继承Object,实现了Serializable和Comparable接口,因此文件支持File对 ...
- MIT-6.828-JOS-lab5:File system, Spawn and Shell
Lab 5: File system, Spawn and Shell tags: mit-6.828 os 概述 本lab将实现JOS的文件系统,只要包括如下四部分: 引入一个文件系统进程(FS进程 ...
随机推荐
- DataPipeline丨新型企业数据融合平台的探索与实践
文 |刘瀚林 DataPipeline后端研发负责人 交流微信 | datapipeline2018 一.关于数据融合和企业数据融合平台 数据融合是把不同来源.格式.特点性质的数据在逻辑上或物理上有机 ...
- redis 特性
Redis 三大特性: Redis 支持数据的持久化,可以将内存中的数据保存在磁盘中,重启的时候可以再次加载进行使用 Redis 不仅支持简单的 键 * 值 类型的数据, 还提供list.set.z ...
- SQL Server作业报错特殊案例
一个作业报错,报错信息如下,从错误信息根本看不出为什么出错,手工运行作业又成功了.一时不清楚什么原因导致作业出错. Message Executed as user: NT SERVICE\SQLSE ...
- Windows 10 ADK 版本及下载链接汇总
Windows 评估和部署工具包 (Windows ADK) 具有自定义大规模部署的 Windows 映像以及测试系统.添加的组件和在该系统上运行的应用程序的质量和性能所需的工具. Windows A ...
- ZJOI2019一轮游记
Preface 期待已久的省选终于开始了233,关于之前的一些内容,在ZJOI2019一轮停课刷题记录都可以找到,这里不再赘述 ZJOI2019,Bless All Day -1 今天难得有休息,昨晚 ...
- 基于HTTP协议的几种实时数据获取技术
原文链接https://www.cnblogs.com/xrq730/p/9280404.html,作者博客园----五月的仓颉,转载请注明出处,谢谢 HTTP协议 HTTP协议大家都很熟悉了,开始本 ...
- nginx部署静态网站
实验环境 服务器:centos7.5 1核1G Nginx版本:nginx-1.14.2 主题 部署静态文件 根据不同url请求路径,定向到不同的系统文件夹 部署静态文件 假设nginx安装在“/us ...
- MacBook IDEA激活码(附视频)
Windows激活请看这里:IDEA激活码 此教程实时更新,请放心使用:如果有新版本出现猪哥都会第一时间尝试激活: idea官网下载地址:https://www.jetbrains.com/idea/ ...
- PERL学习笔记---正则表达式
要匹配某个模式(正则表达式)和$_的关系,可以将模式放在正斜线(//)之间,如下:$_ =“yabba dabba doo”;if(/abba/){print “It matched!\n”;} 表达 ...
- ubuntu 安装 npm、nodejs 各种问题
nodejs let notifier = require('update-notifier')({pkg}) 报错 先卸载nodejs,然后安装稳定最新版 # apt-get remove node ...