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. ...
随机推荐
- 【Spring Boot && Spring Cloud系列】Spring Boot的启动器Starter
Spring Boot的内置Servlet Container: Name Servlet Version Java Version Tomcat8 3.1 Java 7+ Tomcat7 3.0 J ...
- jQuery属性操作(三)
在阅读attr.remove方法时,看到一些对浏览器兼容性做处理的hooks.接下来看一下这些hooks都做了哪些兼容性处理 1.attrHooks.主要处理IE6-9 input的type属性无法写 ...
- filter对数组和对象的过滤
1,对数组的过滤 let arr = ['1', '2', '3'] let b = arr.filter(val => val === '2') console.log(b) // ['2] ...
- [Vue warn]: Error in render: "SyntaxError: Unexpected token ' in JSON at position 1"
一,场景: 字符串转对象: var str = "{'bankRate':5,'YINGUO':0}" 二,操作: JSON.parse(str)时候,报错 [Vue warn]: ...
- sencha touch 自定义cardpanel控件 模仿改进NavigationView 灵活添加按钮组,导航栏,自由隐藏返回按钮(废弃 仅参考)
最新版本我将会放在:http://www.cnblogs.com/mlzs/p/3382229.html这里的示例里面,这里不会再做更新 代码: /* *模仿且改进NavigationView *返回 ...
- VMware ESXI5.5 Memories limits resolved soluation.
在使用VMware ESXI5.5 的时候提示内存限制了,在网上找的了解决方案: 如下文: 1. Boot from VMware ESXi 5.5; 2. wait "Welcome to ...
- HDU 4004 The Frog's Games(二分答案)
The Frog's Games Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65768/65768 K (Java/Others) ...
- GitStack系统RCE漏洞学习
漏洞简介 漏洞简情 漏洞程序 GitStack 影响版本 <=2.3.10 漏洞类型 RCE 漏洞评价 高危 漏洞编号 CVE-2018-5955 漏洞程序介绍 GitStack是一款基于Pyt ...
- zabbix配置server,proxy,agent架构
author: headsen chen date:2018-10-30 19:49:50 环境: centos 6.8_x86_64 zabbix-server: 192.168.1.130 z ...
- Elasticsearch笔记(一)—Elasticsearch安装配置
原文链接:https://my.oschina.net/jhao104/blog/644909 摘要: ElasticSearch是一个基于Lucene的搜索服务器.它提供了一个分布式多用户能力的全文 ...