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. ...
随机推荐
- 【抓包分析】Charles和 夜神模拟器 对安卓应用进行抓包分析
准备工具 : 1 Charles : https://www.charlesproxy.com (收费) 2 夜神模拟器 : https://www.yeshen.com (免费) 2 模拟 ...
- StarUML2 建模工具全平台破解及license验证简要分析
破解方法:找到安装目录下的文件:LicenseManagerDomain.js阅读得知,改软件用rsa加密用户信息,换行为用户信息的分隔符修改以下代码,然后打开软件点击菜单栏中的帮助->输入li ...
- 【Spring源码分析系列】启动component-scan类扫描加载过程
原文地址:http://blog.csdn.net/xieyuooo/article/details/9089441/ 在spring 3.0以上大家都一般会配置一个Servelet,如下所示: &l ...
- 【Spring Boot&&Spring Cloud系列】使用Intellij构建Spring Boot和Mybatis项目
一.创建项目 1.File->New->Project->spring initializer 2.勾选Web SQL Template Engines 3.项目生成之后,点击add ...
- JS案例 - 基于vue的移动端长按手势
================================惯例碎碎念前言================================ 当时首先想到要做长按事件的时候,我想到的是vue内部的自 ...
- 一起脱去小程序的外套和内衣 - 微信小程序架构解析
版权声明:本文由渠宏伟 原创文章,转载请注明出处: 文章原文链接:https://www.qcloud.com/community/article/760767001484042227 来源:腾云阁 ...
- 使用SQLite3工具查看sqlite.db文件
http://www.sqlite.org OS X自从10.4后把SQLite这套相当出名的数据库软件,放进了作业系统工具集里.OS X包装的是第三版的SQLite,又称SQLite3.这套软件有几 ...
- guzzle http异步 post
use GuzzleHttp\Pool;use GuzzleHttp\Client;//use GuzzleHttp\Psr7\Request;use Psr\Http\Message\Respons ...
- 题目1441:人见人爱 A ^ B(二分求幂)
题目链接:http://ac.jobdu.com/problem.php?pid=1441 详解链接:https://github.com/zpfbuaa/JobduInCPlusPlus 参考代码: ...
- mac下安装启动Mongodb
本人最近才上手mac,在使用mac上面有很多不熟悉的地方,慢慢摸索,记录下来,以供后续翻阅与参考: 在Mac下安装MongoDB方式 第一种.用浏览器或者第三方工具下载当前版本的下载地址: http: ...