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. windows下使用git管理代码,其中出现的问题的解决办法

    和朋友共同开发一个小项目,所以就涉及到了代码管理这块,刚开始想到的是使用svn,但是外网访问svn的时候需要使用花生壳来弄一个动态的域名,中间出了很多错误,感觉有点麻烦,所以就想到看看还有别的管理代码 ...

  2. shell截取字符串的一些简单方法

    一.使用${} 1.${var##*/}该命令的作用是去掉变量var从左边算起的最后一个'/'字符及其左边的内容,返回从左边算起的最后一个'/'(不含该字符)的右边的内容.使用例子及结果如下:

  3. 一、K3 Cloud 开发插件《K3 Cloud开发手册》

    1.K3 Cloud打开 提示错误:未能加载文件或程序集“XXXXXXXXX”或它的某一个依赖项 原因:未正常关机导致的Temporary ASP.NET Files文件夹下的文件损坏 解决:删除C: ...

  4. Android.mk(3) 宏

    https://www.jianshu.com/p/7c20b299ee63 传统上我们一直称这种东西为makefile中的变量,其实本质上就是一个宏,只是做的是字符串替换.我们何如就把它叫做宏呢. ...

  5. matlab中 数据保留有效位数

    可以使用round函数  ,这函数原本功能是四舍五入 比如: >> A = 0.0326465;>> B = round(A*1000)/1000 B = 0.0330

  6. 【CF913G】Power Substring 数论+原根

    [CF913G]Power Substring 题意:T组询问,每次给定一个数a,让你求一个k,满足$2^k$的10进制的后$min(100,length(k))$位包含a作为它的子串.你只需要输出一 ...

  7. iOS中self.xxx 和 _xxx 下划线的区别

    property (nonatomic,copy) NSString *propertyName; self.propertyName 是对属性的拜访: _propertyName 是对部分变量的拜访 ...

  8. iOS 8 AutoLayout与Size Class自悟

    前言 iOS8和iPhone6发布已经过去蛮久了,广大的果粉终于迎来了大屏iPhone,再也不用纠结为大屏买三星舍苹果了…但是对于iOS开发人员来说,迎来了和Android开发开发一样的问题—> ...

  9. 2.4scope

    name_scope variable_scope scope (name_scope/variable_scope) from __future__ import print_function im ...

  10. Oracle卸载之Win7操作系统下Oracle11g 数据库卸载过程图解

    1.首先停止oracle11g数据库的5个服务 右键“计算机”,在下拉菜单列表中单击“管理”,进入计算机管理器.图解步骤如下: 选择左侧工具栏最后一项“服务和应用程序”,点击进入下拉菜单,单击“服务” ...