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的更多相关文章

  1. Python实现代码统计工具——终极加速篇

    Python实现代码统计工具--终极加速篇 声明 本文对于先前系列文章中实现的C/Python代码统计工具(CPLineCounter),通过C扩展接口重写核心算法加以优化,并与网上常见的统计工具做对 ...

  2. 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] ...

  3. [Android Pro] 自己动手编译Android源码(超详细)

    cp from : https://www.jianshu.com/p/367f0886e62b 在Android Studio代码调试一文中,简单的介绍了代码调试的一些技巧.现在我们来谈谈andro ...

  4. 自己动手编译Android源码(超详细)

    http://www.jianshu.com/p/367f0886e62b 在Android Studio代码调试一文中,简单的介绍了代码调试的一些技巧.现在我们来谈谈android源码编译的一些事. ...

  5. 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 ...

  6. 使用IExport进行图片输出出现File creation error

    使用IExport进行图片输出(.JPG)时,出现如下异常File creation error.   在ESRI.ArcGIS.Output.ExportJPEGClass.FinishExport ...

  7. 问题解决: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 ...

  8. ARM compiler No such file or directory

    /********************************************************************************* * ARM compiler No ...

  9. 利用Delphi的File Of Type创建并管理属于你自己的数据库

    http://www.360doc.com/content/16/1128/19/28222077_610249962.shtml 利用Delphi的File Of Type创建并管理属于你自己的数据 ...

  10. Python:IOError: image file is truncated 的解决办法

    代码如下: #coding:utf-8 from PIL import Image import pytesseract def test(): im = Image.open(r"pic. ...

随机推荐

  1. 【CSS系列】height:100%设置div的高度

    一.div设置百分百高度实现描述 在html布局中body内第一个div盒子对象设置100%高度height样式,是无法成功显示100%高度的.这个是因为body高度默认值为自适应的,所以及时设置bo ...

  2. WPS 2019 去除自动升级 和 广告、及优化的点

    搜狗输入法 里面的快捷键会影响wps的快捷键功能,需要关掉"搜狗输入法"里面的快捷键 1. 2.去除自动升级功能 3.去除 广告 WPS 2019 流程图(断网): 思维导图: 流 ...

  3. Android.mk (1) 函数

    https://www.jianshu.com/p/46224d15fc5f 从函数说起 大家都习惯看从头,从构建目标讲起的,导致每篇文档熟的都是前面的部分.很多教程也都是想办法能够观其大略,从整体上 ...

  4. ORA-00600: internal error code, arguments: [kgl-no-mutex-held]

    一.环境 windows oracle 11.2.0.4 RAC 二.问题现象 1.连接数据库后,无法查询 2.报错信息:ORA-00600: internal error code, argumen ...

  5. BZOJ3163&Codevs1886: [Heoi2013]Eden的新背包问题[分治优化dp]

    3163: [Heoi2013]Eden的新背包问题 Time Limit: 10 Sec  Memory Limit: 256 MBSubmit: 428  Solved: 277[Submit][ ...

  6. NET的堆和栈04,对托管和非托管资源的垃圾回收以及内存分配

    在" .NET的堆和栈01,基本概念.值类型内存分配"中,了解了"堆"和"栈"的基本概念,以及值类型的内存分配.我们知道:当执行一个方法的时 ...

  7. ThreadLocal Java并发

    ThreadLocal 文章来源:http://con.zhangjikai.com/ThreadLocal.html ThreadLocal 主要用来提供线程局部变量,也就是变量只对当前线程可见. ...

  8. java不足前面补0

    // 0 代表前面补充0 // 3代表长度为3 // d 代表参数为正数型 result=String.format("%0"+3+"d",result);

  9. TOP100summit:【分享实录】爆炸式增长的斗鱼架构平台的演进

    本篇文章内容来自2016年TOP100summit斗鱼数据平台部总监吴瑞城的案例分享. 编辑:Cynthia 吴瑞诚:斗鱼数据平台部总监 曾先后就职于淘宝.一号店. 从0到1搭建公司大数据平台.平台规 ...

  10. PAT-GPLT L1-033 - 出生年 - [简单模拟]

    题目链接:https://www.patest.cn/contests/gplt/L1-033 时间限制 400 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standar ...