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的更多相关文章

  1. python file operations

    原文地址 总是记不住API.昨晚写的时候用到了这些,但是没记住,于是就索性整理一下吧: python中对文件.文件夹(文件操作函数)的操作需要涉及到os模块和shutil模块. 得到当前工作目录,即当 ...

  2. File Operations

    在刷题测试程序时,为了避免每次都手工输入,我们可以把输入数据保存在文件中:为了避免输出太长,我们将输出也写入文件中,方便与标准答案文件进行比较. 文件使用一般有两种方法:输入输出重定向.fopen. ...

  3. 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> ...

  4. 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 ...

  5. Python File I/O

    File is a named location on disk to store related information. It is used to permanently store data ...

  6. File Operation using SHFileOperation

    SHFILEOPSTRUCT Original link: http://winapi.freetechsecrets.com/win32/WIN32SHFILEOPSTRUCT.htm Refere ...

  7. python文件和文件夹訪问File and Directory Access

    http://blog.csdn.net/pipisorry/article/details/47907589 os.path - Common pathname manipulations 都是和路 ...

  8. Java-Io之文件File

    File是"文件"和"目录路径名"的抽象表示形式.File之间继承Object,实现了Serializable和Comparable接口,因此文件支持File对 ...

  9. 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进程 ...

随机推荐

  1. K3数据字典备查

    select distinct f.FNumber as 系统代码, f.FName AS 系统名称,  d.FTableName AS 表名,d.FDescription AS 表说明,a.[nam ...

  2. express+handlebars 快速搭建网站前后台

    最近在重构公司网站,原来网站使用PHP,前后端不分离,添加与更新网站内容仍使用原始方法,先出布局再把调好的布局给PHP后端开发,花时间长,维护不易.因此决定将网站前后端分离,核心功能含网站下单及CRM ...

  3. 第三周LINUX学习笔记

    周期性任务丶find 文件查找:find命令 locate :在数据库中查找,非实时查找,精确度不高,查找速度快,模糊查找  /tmp/passwad/a.textfind:实时查找:速度慢  ,精确 ...

  4. 20170310 - Python 3 下 SQLAlchemy 的 MySQL 数据库 URI 配置

    MySQL-Python 只用于 Python 2,URI配置为 mysql://username:password@server/db Python 3 下要使用另一个 PyMySQL 包,相应的U ...

  5. winfrom SVG转Imge

    svg矢量图的使用,将svg矢量图展示在pictureBox上,拖动可以应用到其他设计软件上,复杂一点,中间涉及到SVG的下载  以及  SVG转化为 图片等操作 效果图如下: 源码下载地址: htt ...

  6. 使用async await 封装 axios

    异步变同步: 厉害-- https://www.jianshu.com/p/4168efdc172b

  7. SpringBoot2.0之五 优雅整合SpringBoot2.0+MyBatis+druid+PageHelper

    上篇文章我们介绍了SpringBoot和MyBatis的整合,可以说非常简单快捷的就搭建了一个web项目,但是在一个真正的企业级项目中,可能我们还需要更多的更加完善的框架才能开始真正的开发,比如连接池 ...

  8. 视频直播 object 标签属性详解

    最近在做视频直播这一块的,html5的video不能实现此功能,在网上查找了资料,觉得很有用. 一.介绍: 我们要在网页中正常显示flash内容,那么页面中必须要有指定flash路径的标签.也就是OB ...

  9. 说一下Dubbo 的工作原理?注册中心挂了可以继续通信吗?

    面试题 说一下的 dubbo 的工作原理?注册中心挂了可以继续通信吗?说说一次 rpc 请求的流程? 面试官心理分析 MQ.ES.Redis.Dubbo,上来先问你一些思考性的问题.原理,比如 kaf ...

  10. 微信小程序开发01-小程序的执行流程是怎么样的?

    前言 我们这边最近一直在做基础服务,这一切都是为了完善技术体系,这里对于前端来说便是我们需要做一个Hybrid体系,如果做App,React Native也是不错的选择,但是一定要有完善的分层: ① ...