文件操作

文件路径

d:\test.txt

编码方式

utf-8 gbk。。。

操作方式

操作方式:只读,只写,追加,读写,写读.....

以什么编码方式储存的文件,就以什么编码打开进行操作。

    只读:r rb
f = open('阿卡丽',mode='r',encoding='utf-8')
content = f.read()
print(content,type(content))
f.close() # f = open('阿卡丽',mode='rb',)
# content = f.read()
# print(content)
# f.close() 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()
只写: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()
w+
# f = open('log',mode='w+',encoding='utf-8')
# f.write('aaa')
# f.seek(0)
# print(f.read())
# f.close()
w+b
.......
追加
# f = open('log',mode='a',encoding='utf-8')
# f.write('鬼娃')
# f.close() # f = open('log',mode='ab')
# f.write('鬼娃'.encode('utf-8'))
# f.close()
#绝对路径
# f = open('d:\阿卡丽.txt',mode='r',encoding='UTF-8')
# content = f.read()
# print(content)
# f.close() #bytes ---->str
# f = open('阿卡丽',mode='r',encoding='utf-8')
# content = f.read()
# f.write('fjsdlk')
# f.close() # f = open('阿卡丽',mode='rb',)
# content = f.read()
# print(content)
# f.close()
# f = open('log',mode='r+',encoding='utf-8')
# print(f.read())
# f.close() # f = open('log',mode='r+b')
# print(f.read())
# f.write('大猛,小孟'.encode('utf-8'))
# f.close() #对于w:没有此文件就会创建文件
# f = open('log',mode='w',encoding='utf-8')
# f.write('骑兵步兵')
# f.close() # 先将源文件的内容全部清除,在写。
# f = open('log',mode='w',encoding='utf-8')
# f.write('附近看到类似纠纷')
# f.close() # f = open('log',mode='w+',encoding='utf-8')
# f.write('aaa')
# f.seek(0)
# print(f.read())
# f.close() # f = open('log',mode='wb')
# f.write('附近看到类似纠纷'.encode('utf-8'))
# f.close() # f = open('log',mode='a',encoding='utf-8')
# f.write('鬼娃')
# f.close()
#
# f = open('log',mode='a',encoding='utf-8')
# f.write('鬼娃')
# f.close() # f = open('log',mode='a+',encoding='utf-8')
# f.write('鬼娃')
# f.seek(0)
# print(f.read())
# f.close() # f = open('log',mode='ab')
# f.write('鬼娃'.encode('utf-8'))
# f.close()
#功能详解

# 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:

day009 文件操作的更多相关文章

  1. 【.NET深呼吸】Zip文件操作(1):创建和读取zip文档

    .net的IO操作支持对zip文件的创建.读写和更新.使用起来也比较简单,.net的一向作风,东西都准备好了,至于如何使用,请看着办. 要对zip文件进行操作,主要用到以下三个类: 1.ZipFile ...

  2. 野路子出身PowerShell 文件操作实用功能

    本文出处:http://www.cnblogs.com/wy123/p/6129498.html 因工作需要,处理一批文件,本想写C#来处理的,后来想想这个是PowerShell的天职,索性就网上各种 ...

  3. Node基础篇(文件操作)

    文件操作 相关模块 Node内核提供了很多与文件操作相关的模块,每个模块都提供了一些最基本的操作API,在NPM中也有社区提供的功能包 fs: 基础的文件操作 API path: 提供和路径相关的操作 ...

  4. 归档NSKeyedArchiver解归档NSKeyedUnarchiver与文件管理类NSFileManager (文件操作)

    ========================== 文件操作 ========================== 一.归档NSKeyedArchiver 1.第一种方式:存储一种数据. // 归档 ...

  5. SQL Server附加数据库报错:无法打开物理文件,操作系统错误5

    问题描述:      附加数据时,提示无法打开物理文件,操作系统错误5.如下图: 问题原因:可能是文件访问权限方面的问题. 解决方案:找到数据库的mdf和ldf文件,赋予权限即可.如下图: 找到mdf ...

  6. 通过cmd完成FTP上传文件操作

    一直使用 FileZilla 这个工具进行相关的 FTP 操作,而在某一次版本升级之后,发现不太好用了,连接老是掉,再后来完全连接不上去. 改用了一段时间的 Web 版的 FTP 工具,后来那个页面也 ...

  7. Linux文件操作的主要接口API及相关细节

    操作系统API: 1.API是一些函数,这些函数是由linux系统提供支持的,由应用层程序来使用,应用层程序通过调用API来调用操作系统中的各种功能,来干活 文件操作的一般步骤: 1.在linux系统 ...

  8. C语言的fopen函数(文件操作/读写)

    头文件:#include <stdio.h> fopen()是一个常用的函数,用来以指定的方式打开文件,其原型为:    FILE * fopen(const char * path, c ...

  9. Python的文件操作

    文件操作,顾名思义,就是对磁盘上已经存在的文件进行各种操作,文本文件就是读和写. 1. 文件的操作流程 (1)打开文件,得到文件句柄并赋值给一个变量 (2)通过句柄对文件进行操作 (3)关闭文件 现有 ...

随机推荐

  1. 20180703mysql运维专题一:利用etl监控mysql日志

    参考地址: https://www.elastic.co/solutions/logging https://www.elastic.co/guide/en/beats/filebeat/curren ...

  2. [bzoj2989]数列_KD-Tree_旋转坐标系

    数列 bzoj-2989 题目大意:题目链接. 注释:略. 想法:显然,我们用x和a[x]两个值建立笛卡尔坐标系. 两个点之间的距离为曼哈顿距离. 修改操作就是插入... 查询操作就是查询一个点周围的 ...

  3. centos7 安装vsftpd的步骤

    感觉非常坑,依照网上说的没一个都測试了,可一直都报错. 不断的又一次安装不下10次,最后一次最终測试出了正确的方法. #官网配置说明## https://security.appspot.com/vs ...

  4. CF #316 DIV2 D题

    D. Tree Requests time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  5. 1.7-BGP⑥

    BGP中的路由控制/过滤: LAB1:Distribute-list调用ACL(较落后) ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~      Step1:通过ACL定义 ...

  6. 瀑布流 ajax 预载入 json

    pbl.json[模拟后台json数据]: [     {         "id": "511895",         "title": ...

  7. POJ 3344 &amp; HDU 2414 Chessboard Dance(模拟)

    题目链接: PKU:http://poj.org/problem? id=3344 HDU:http://acm.hdu.edu.cn/showproblem.php?pid=2414 Descrip ...

  8. 【SDOI2008】【BZOJ2049】Cave 洞穴勘測

    Description 辉辉热衷于洞穴勘測.某天,他依照地图来到了一片被标记为JSZX的洞穴群地区.经过初步勘測,辉辉发现这片区域由n个洞穴(分别编号为1到n)以及若干通道组成.而且每条通道连接了恰好 ...

  9. 欧几里得 &amp; 拓展欧几里得算法 解说 (Euclid &amp; Extend- Euclid Algorithm)

    欧几里得& 拓展欧几里得(Euclid & Extend-Euclid) 欧几里得算法(Euclid) 背景: 欧几里德算法又称辗转相除法.用于计算两个正整数a.b的最大公约数. -- ...

  10. 抽象类(Abstract)和接口的不同点、共同点(Interface)。

    同样点: (1) 都能够被继承 (2) 都不能被实例化 (3) 都能够包括方法声明 (4) 派生类必须实现未实现的方法 区 别: (1) 抽象基类能够定义字段.属性.方法实现.接口仅仅能定义属性.索引 ...