文件操作

文件路径

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. U-Boot> help, 命令集

    U-Boot> help ?       - alias for 'help' base    - print or set address offset boot    - boot defa ...

  2. github的submodel错误

    原文地址 简要:直接把github上的仓库删除重建,本地的仓库也删除重建,再重新上传就OK了 最近想整理一下工作室官网的东西,那是一个用github pages写的网站,在上传一些post文章的时候, ...

  3. 软件测试之怎么避免Bug漏测?

    一.对需求评审阶段,对业务需求细节理解不明确,未深入挖掘隐含拓展需求 改进措施 需求评审前,我们应该先仔细阅读prd及交互文档,先形成自己对产品的思考,通过脑图的方式列出对产品设计的疑问点,从用户或者 ...

  4. [Tools] Using colours in a NodeJS terminal - make your output pop

    Use can use colour and styles to make it easy to spot errors and group common functionality into blo ...

  5. scp and tar

    scp 命令随记 scp file username@remoteIp:directory 创建tar包 tar zcvf file.tar.gz directory tar zcvf hadoop. ...

  6. hdu 6082 度度熊与邪恶大魔王(2017"百度之星"程序设计大赛 - 资格赛 )

    度度熊与邪恶大魔王 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total S ...

  7. hdu1150——最小点覆盖

    As we all know, machine scheduling is a very classical problem in computer science and has been stud ...

  8. AAC帧格式及编码介绍

    参考资料: AAC以adts格式封装的分析:http://wenku.baidu.com/view/45c755fd910ef12d2af9e74c.html aac编码介绍:http://wenku ...

  9. Websocket实现前后台通信,demo小测试

    新需求大概如下:用户登录系统,登录成功之后建立websocket连接,实现通信 总体思路:前端不是我负责,只是简单的做个功能,先实现登录,把用户标识存入HttpSeesion,再建立websocket ...

  10. B - Helpful Maths

    Problem description Xenia the beginner mathematician is a third year student at elementary school. S ...