0627-File-163
print('创建一个文件向其中写入信息,再读取出写入的信息-------------------------------------')
f = open('E:\\foo4.txt','w+')
f.write('第1行:呵呵哒001。\n第2行:呵呵哒002。\n第3行:呵呵哒003。\n第4行:呵呵哒004。\n')
f = open('E:\\foo4.txt','r')
s4 = f.readlines()
print(s4)
f.close()
for index in range(5):
line = next(f)
print('第{0}行:{1}'.format(index,line))
print('')
print('close()的用法演示-----------------------------------------------------------------')
fo = open('Runoob.txt','wb')
print('文件名为:',fo.name)
fo.flush()
#检测文件是否连接到终端设备
ret = fo.isatty()
print('返回值:',ret)
fo.close()
print('f.read()的用法-----------------------------------------------------------------------')
f = open('E:\\foo5.txt','w+')
f.write('第1行:呵呵哒001。\n第2行:呵呵哒002。\n第3行:呵呵哒003。\n第4行:呵呵哒004。\n')
f = open('E:\\foo5.txt','r')
s4 = f.readlines()
print(s4)
line = f.read(10)
print('读取的字符串为:{0}'.format(line))
f.close()
print('f.read()读取字符串------------------------------------------------------------------')
f = open('E:\\foo5.txt','w+')
f.write('第1行:呵呵哒001。\n第2行:呵呵哒002。\n第3行:呵呵哒003。\n第4行:呵呵哒004。\n')
f = open('E:\\foo5.txt','r')
# s4 = f.readlines()
# print(s4)
line = f.read(20)
print('读取的字符串为:{0}'.format(line))
f.close()
print('f.readline()读取字符串------------------------------------------------------------------')
f = open('E:\\foo6.txt','w+')
f.write('第1行:呵呵哒001。\n第2行:呵呵哒002。\n第3行:呵呵哒003。\n第4行:呵呵哒004。\n')
f = open('E:\\foo6.txt','r')
print('文件名为:{}'.format(f.name))
for line in f.readlines():
line = line.strip()
print('读取的数据为:{}'.format(line))
f.close()
print('f.readline()读取字符串------------------------------------------------------------------')
f = open('E:\\foo6.txt','w+')
f.write('第1行:呵呵哒001。\n第2行:呵呵哒002。\n第3行:呵呵哒003。\n第4行:呵呵哒004。\n')
f = open('E:\\foo6.txt','r')
print('文件名为:{}'.format(f.name))
line = f.readline()
print('1读取的数据为:{}'.format(line))
# f.seek(offset, from_what) 函数,from_what 的值, 如果是 0 表示开头, 如果是 1 表示当前位置, 2 表示文件的结尾
f.seek(0,1)
line = f.readline()
print('2读取的数据为:{}'.format(line))
print('f.tell()返回文件的当前位置,即文件指针当前位置---------------------------------------------------------------')
f = open('E:\\foo6.txt','w+')
f.write('第1行:呵呵哒001。\n第2行:呵呵哒002。\n第3行:呵呵哒003。\n第4行:呵呵哒004。\n')
f = open('E:\\foo6.txt','r')
print('文件名为:{}'.format(f.name))
line = f.readline()
print('1读取的数据为:{}'.format(line))
#获取当前文件位置
pos = f.tell()
print('当前位置:{0}'.format(pos))
f.close()
print('f.truncate()方法---------------------------------------------------------------')
#创建文件及写入信息
f = open('E:\\foo8.txt','w+')
f.write('第1行:呵呵哒001。\n第2行:呵呵哒002。\n第3行:呵呵哒003。\n第4行:呵呵哒004。\n')
#打开文件
f = open('E:\\foo8.txt','r+')
# f = open('E:\\foo8.txt','r')#这样写会报错:io.UnsupportedOperation: File not open for writing
print('文件名为:{}'.format(f.name))
#截取10个字节
f.truncate(20)
line = f.readlines()
print('1读取的数据为:{}'.format(line))
#关闭文件
f.close()
print('f.write()方法---------------------------------------------------------------')
#创建文件及写入信息
f = open('E:\\foo11.txt','w+')
f.write('第1行:呵呵哒001。\n第2行:呵呵哒002。\n第3行:呵呵哒003。\n第4行:呵呵哒004。\n')
#打开文件
f = open('E:\\foo11.txt','r+')
print('文件名为:{0}'.format(f.name))
#在文件的末尾添加一行
str = '第5行:呵呵哒005'
line = f.write(str)
#读取文件中所有内容
linea = f.read()
print('读取的数据为:{0}'.format(linea))#为什么读取不出第5行追加的内容呢?????
#关闭文件
f.close()
print('f.writelines()方法---------------------------------------------------------------')
#创建文件写入序列信息
f = open('E:\\foo13.txt','w+')
seq= ['菜鸟教程1\n','菜鸟教程2']
f.writelines(seq)
f = open('E:\\foo13.txt','r+')
print('文件名为{0}'.format(f.name))
#读取文件中所有内容
lines = f.readlines()
print('读取的数据为:{0}'.format(lines))
#关闭文件
f.close()
今天不太在状态,坐等下班,困困哒~
0627-File-163的更多相关文章
- Python实现代码统计工具——终极加速篇
Python实现代码统计工具--终极加速篇 声明 本文对于先前系列文章中实现的C/Python代码统计工具(CPLineCounter),通过C扩展接口重写核心算法加以优化,并与网上常见的统计工具做对 ...
- Background Media Recovery terminated with ORA-1274 after adding a Datafile (Doc ID 739618.1)
APPLIES TO: Oracle Database - Enterprise Edition - Version 9.2.0.1 to 12.1.0.2 [Release 9.2 to 12.1] ...
- [Android Pro] 自己动手编译Android源码(超详细)
cp from : https://www.jianshu.com/p/367f0886e62b 在Android Studio代码调试一文中,简单的介绍了代码调试的一些技巧.现在我们来谈谈andro ...
- 自己动手编译Android源码(超详细)
http://www.jianshu.com/p/367f0886e62b 在Android Studio代码调试一文中,简单的介绍了代码调试的一些技巧.现在我们来谈谈android源码编译的一些事. ...
- Binary XML file line #2: Error inflating
06-27 14:29:27.600: E/AndroidRuntime(6936): FATAL EXCEPTION: main 06-27 14:29:27.600: E/AndroidRunti ...
- 使用IExport进行图片输出出现File creation error
使用IExport进行图片输出(.JPG)时,出现如下异常File creation error. 在ESRI.ArcGIS.Output.ExportJPEGClass.FinishExport ...
- 问题解决:psql: could not connect to server: No such file or directory Is the server running locally and accepting connections on Unix domain socket "/var/run/postgresql/.s.PGSQL.5432"?
错误提示: psql: could not connect to server: No such file or directory Is the server running locally and ...
- ARM compiler No such file or directory
/********************************************************************************* * ARM compiler No ...
- 利用Delphi的File Of Type创建并管理属于你自己的数据库
http://www.360doc.com/content/16/1128/19/28222077_610249962.shtml 利用Delphi的File Of Type创建并管理属于你自己的数据 ...
- Python:IOError: image file is truncated 的解决办法
代码如下: #coding:utf-8 from PIL import Image import pytesseract def test(): im = Image.open(r"pic. ...
随机推荐
- vue经验 - 实战疑点总结
1.注册全局组件(是一个单vue页面组成的一个组件,而不是现拼的template结构) 结构: 代码:main.js import UserList from './components/UserLi ...
- Redis学习笔记--Redis配置文件redis.conf参数配置详解
########################################## 常规 ########################################## daemonize n ...
- SSH用户枚举漏洞(CVE-2018-15473)原理学习
一.漏洞简介 1.漏洞编号和类型 CVE-2018-15473 SSH 用户名(USERNAME)暴力枚举漏洞 2.漏洞影响范围 OpenSSH 7.7及其以前版本 3.漏洞利用方式 由于SSH本身的 ...
- 删除个别主机的Know_hosts文件信息
方法一: rm -rf ~/.ssh/known_hosts 缺点:把其他正确的公钥信息也删除,下次链接要全部重新经过认证 方法二: vi ~/.ssh/known_hosts 删除对应ip的相关rs ...
- Dockerfile ,ADD详细解读
一.ADD指令 ADD指令的功能是将主机构建环境(上下文)目录中的文件和目录.以及一个URL标记的文件 拷贝到镜像中. 其格式是: ADD 源路径 目标路径 如: #test FROM ubunt ...
- asp.net搭建mybatis开发环境
mybatis其实就是ibatis的升级版本不仅能在java上使用,asp.net照样可以使用mybatis来开发程序.mybatis是一个比较小巧的ORM框架,类似hibernate.自己试了一下用 ...
- Ios8 Xcode6 设置Launch Image 启动图片
http://blog.sina.com.cn/s/blog_6c97abf10102voui.html Http://Www.woowen.com/Swift/2014/12/12/Ios8设置La ...
- 画面渲染:实时渲染(Real-time Rendering)、离线渲染(Offline Rendering)[转]
实时渲染(Real-time Rendering) 实时渲染的本质就是图形数据的实时计算和输出.最典型的图形数据源是顶点.顶点包括了位置.法向.颜色.纹理坐标.顶点的权重等.在第一代渲染技术中(198 ...
- 思科SVI接口和路由接口区别
Cisco多层交换中提到了一个SVI接口,路由接口.在多层交换机上可以将端口配置成不同类型的接口. 其中SVI接口 类似于 interface Vlan10ip address 192.168.20 ...
- Spark2 DataSet 创建新行之flatMap
val dfList = List(("Hadoop", "Java,SQL,Hive,HBase,MySQL"), ("Spark", & ...