文件操作

文件路径

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. in与exists的区别

    转载自:http://blog.csdn.net/lick4050312/article/details/4476333 select * from Awhere id in(select id fr ...

  2. 从事分布式系统,计算,hadoop

    作者:廖君链接:https://www.zhihu.com/question/19868791/answer/88873783来源:知乎 分布式系统(Distributed System)资料 < ...

  3. iOS:制作左右侧滑(抽屉式)菜单

    感谢控件作者:https://github.com/SocialObjects-Software/AMSlideMenu 首先上效果图: 这里我们使用AMSlideMenu来实现左右侧滑菜单的效果.控 ...

  4. HDU 1171 Big Event in HDU(01背包)

    题目地址:HDU 1171 还是水题. . 普通的01背包.注意数组要开大点啊. ... 代码例如以下: #include <iostream> #include <cstdio&g ...

  5. QFileDialog关于选择文件对话框中的几个信号的说明(currentChanged,directoryEntered,fileSelected,filterSelected)

    QFileDialog关于选择文件对话框中的几个信号 实例: openFile::openFile(QWidget *parent) :QWidget(parent),ui(new Ui::openF ...

  6. Android EditText得到和失去焦点时,自定义处理内容

    当android的Edittext得到/失去焦点时,需要自定义一些处理内容时,需要对EditText对象的Focus进行监听处理. 在Activity中,做以下处理: 1.取得EditText对象. ...

  7. [RK3288][Android6.0] 调试笔记 --- 系统第一次开机进入Recovery模式原因【转】

    本文转载自:http://blog.csdn.net/kris_fei/article/details/53464461 latform: ROCKCHIPOS: Android 6.0Kernel: ...

  8. SQL Server 2005数据库定期备份(非常详细)与 SQL Server 2005数据库备份定期清理

     SQL Server 2005数据库定期备份 分类: SQL Server 20052011-01-06 16:25 3320人阅读 评论(1) 收藏 举报 sql server数据库sqlserv ...

  9. HTML多媒体标记之字幕标记

    在HTML中,可以向页面中插入字幕,水平或垂直滚动显示文字信息,字幕标记的格式如下: <marquee 属性="值"...>滚动的文字信息</marquee> ...

  10. 搭建go开发环境

    一.go下载安装 进入该网站 https://golang.google.cn/dl/  选择相应的操作系统下载安装包 Linux/Mac OS X 安装 1.下载 go1.10.3.linux-am ...