'''
file(文件)方法
'''
#open()对象 pass
#file对象 ##file.close():关闭文件,关闭后不能再进行读写操作
fo1 = open('/Users/ligaijiang/PycharmProjects/fo1.txt','wb')
print('文件名为:',fo1.name)
fo1.close() ##file.flush()
fo2 = open('/Users/ligaijiang/PycharmProjects/fo2.txt','wb')
print('文件名为:',fo2.name)
fo2.flush()
fo2.close() ##file.fileno()
fo3 = open('/Users/ligaijiang/PycharmProjects/fo3.txt','wb')
print('文件名为:',fo3.name) fid = fo3.fileno()
print('文件描述为:',fid)
fo3.close() ##file.isatty()
fo4 = open('/Users/ligaijiang/PycharmProjects/fo4.txt','wb')
print('文件名为:',fo4.name) ret = fo4.isatty()
print('返回值:',ret)
fo3.close() ##file.next()
f4 = open('/Users/ligaijiang/PycharmProjects/fo4.txt','w')
f4.write('这是第一行\n这是第二行\n这是第三行\n这是第四行\n这是第五行\n')
f4.close() fo4 = open('/Users/ligaijiang/PycharmProjects/fo4.txt','r')
print('文件名为:',fo4.name)
for index in range(5):
line = next(fo4)
print('第{}行——————{}'.format(index,line))
fo4.close() ##file.read()
f5 = open('/Users/ligaijiang/PycharmProjects/fo5.txt','w')
f5.write('这是第一行。\n这是第二行。\n这是第三行。\n这是第四行。\n这是第五行。\n')
f5.close() f5 = open('/Users/ligaijiang/PycharmProjects/fo5.txt','r')
print('文件名为:',f5.name) line = f5.read(10)
print('读取第字符串为:',line)
f5.close() ##file.readline()
f6 = open('/Users/ligaijiang/PycharmProjects/fo6.txt','w')
value = 'www.runoob.com'
s = str(value) n = 0
for n in range(5):
f6.write('{}\n'.format(s))
n =n+1
f6.close() f6 = open('/Users/ligaijiang/PycharmProjects/fo6.txt','r+')
print('文件名为:',f6.name) line1 = f6.readline()
print('f6读取第一行为{}:'.format(line1)) line2 = f6.readline(5)
print('f6读取字符串为{}:'.format(line2)) f6.close() ##file.readlines()
f7 = open('/Users/ligaijiang/PycharmProjects/fo7.txt','w')
value = 'www.runoob.com'
s = str(value) n = 0
for n in range(5):
f7.write('{}\n'.format(s))
n =n+1
f7.close() f7 = open('/Users/ligaijiang/PycharmProjects/fo7.txt','r+')
print('文件名为f7:',f7.name) for line3 in f7.readlines():
line3 = line3.strip()
print('读取第数据为:{}'.format(line3))
f7.close() ##file.seek()
f8 = open('/Users/ligaijiang/PycharmProjects/fo8.txt','w')
value = 'www.runoob.com'
s = str(value) n = 0
d = 1
for n in range(5):
f8.write('第{0}行:{1}\n'.format(d,s))
n =n+1
d = d+1
f8.close() f8 = open('/Users/ligaijiang/PycharmProjects/fo8.txt','r+')
print('文件名为f8:',f8.name) line81 = f8.readline()
print('line81读到的字符串为:',line81) line82 = f8.readline()
print('line82读到的字符串为:',line82) f8.seek(0,0)
line83 = f8.readline()
print('line83读到的字符串为:',line83) f8.close() ##file.tell():获取文件当前位置
f9 = open('/Users/ligaijiang/PycharmProjects/fo9.txt','w')
value = 'www.runoob.com'
s = str(value) n = 0
d = 1
for n in range(5):
f9.write('第{0}行:{1}\n'.format(d,s))
n =n+1
d = d+1
f9.close() f9 = open('/Users/ligaijiang/PycharmProjects/fo9.txt','r+')
print('文件名为f8:',f9.name) line91 = f9.readline()
print('line91读到的字符串为:',line91) pos = f9.tell()
print('当前位置为{}'.format(pos)) f9.close() ##file.truncate([size])
f10 = open('/Users/ligaijiang/PycharmProjects/fo10.txt','w')
value = 'www.runoob.com'
s = str(value) n = 0
d = 1
for n in range(5):
f10.write('第{0}行:{1}\n'.format(d,s))
n =n+1
d = d+1
f10.close() f10 = open('/Users/ligaijiang/PycharmProjects/fo10.txt','r+')
print('文件名为f8:',f10.name) line101 = f10.readline()
print('line101读到的字符串为:',line101) f10.truncate()
line102 = f10.readlines()
print('line102读取行{}'.format(line102)) f10.truncate(10)
line103 = f10.readlines()
print('line103读取行{}'.format(line103)) f9.close() #
f11 = open('/Users/ligaijiang/PycharmProjects/fo11.txt','w')
value = 'www.runoob.com'
s = str(value) n = 0
d = 1
for n in range(5):
f11.write('第{0}行:{1}\n'.format(d,s))
n =n+1
d = d+1
f11.close() f11 = open('/Users/ligaijiang/PycharmProjects/fo11.txt','r+')
print('文件名为f11:',f11.name) f11.truncate(10)
str1 = f11.read()
print('--读取行:{}'.format(str1)) f11.close() ##file.write():
f12 = open('/Users/ligaijiang/PycharmProjects/fo12.txt','w')
value = 'www.runoob.com'
s = str(value) n = 0
d = 1
for n in range(5):
f12.write('第{0}行:{1}\n'.format(d,s))
n =n+1
d = d+1
f12.close() f12 = open('/Users/ligaijiang/PycharmProjects/fo12.txt','r+')
print('文件名为f12:',f12.name) str2 = '第6行:www.runoob.com'
f12.seek(0,2)
line121=f12.write(str2) f12.seek(0,0)
for index in range(6):
line122 = next(f12)
print('文件行号{}-----{}'.format(index,line122))
f12.close() ##file.writelines()
f13 =open('/Users/ligaijiang/PycharmProjects/fo13.txt','w')
print('文件名为:',f13.name)
seq = ['菜鸟教程1\n','菜鸟教程2\n','菜鸟教程3\n']
f13.writelines(seq)
f13.close()

23-Python3 File的更多相关文章

  1. Python3 File 方法

    Python3 File(文件) 方法 file 对象使用 open 函数来创建,下表列出了 file 对象常用的函数: 序号 方法及描述 1 file.close() 关闭文件.关闭后文件不能再进行 ...

  2. Ubuntu16.04Server版离线安装Nginx1.8.1+Mysql5.7.23+Python3.6.2

    nginx1.8.1 1.安装前准备工作 1.1.检查系统版本,确认源码编译所依赖的环境,提前下载好压缩包. 整个环境都是使用root权限安装,系统版本为server版的ubuntu16.04.4 r ...

  3. Python3 File

    open() 方法 Python open() 方法用于打开一个文件,并返回文件对象,在对文件进行处理过程都需要使用到这个函数,如果该文件无法被打开,会抛出 OSError. 注意:使用 open() ...

  4. python019 Python3 File(文件) 方法

    file 对象使用 open 函数来创建,下表列出了 file 对象常用的函数: 序号 方法及描述 1 file.close() 关闭文件.关闭后文件不能再进行读写操作. 2 file.flush() ...

  5. 吴裕雄--天生自然python学习笔记:Python3 File(文件) 方法

    open() 方法 Python open() 方法用于打开一个文件,并返回文件对象,在对文件进行处理过程都需要使用到这个函数,如果该文件无法被打开,会抛出 OSError. 注意:使用 open() ...

  6. python 输入输出,file, os模块

    Python 输入和输出 输出格式美化 Python两种输出值的方式: 表达式语句和 print() 函数. 第三种方式是使用文件对象的 write() 方法,标准输出文件可以用 sys.stdout ...

  7. python3之循环

    本节主要介绍python中循环语句的用法以及其他一些语句exec等,文章后面附有之前的文章: 循环语句 if是值执行一次而循环时在条件满足情况下可以执行相同语句多次:使用循环可以轻松实现序列,词典等的 ...

  8. Python3.6安装protobuf模块+将proto文件转换成pb2.py文件

    Python对版本的对应即为苛刻,笔者第一次安装时遇到了很多坑,比如无法将proto文件转换成py文件,转换了之后文件无法使用,网上各种各样的解决办法都没有讲到重点.其实会出现各种各样的问题是由于版本 ...

  9. convert \uXXXX String to Unicode Characters in Python3.x

    转换\uXXXX if Python3.x: str.decode no longer exists in 3.x. that']s why Python 3.4: str : AttributeEr ...

  10. python第七十课——python2与python3的一些区别

    1.性能:py3.x起始比py2.x效率低,但是py3.x有极大的优化空间,效率正在追赶 2.编码:py3.x原码文件默认使用utf-8编码,使得变量名更为广阔 中国='CHI' print(中国) ...

随机推荐

  1. git rebase 操作撤销

    git rebase可以更改提交历史,在不影响别人的情况下,能够重整git树. 但如果git rebase操作失误,却在push后才发现,怎么撤销rebase操作呢? 使用git reflog + g ...

  2. Entity Framework DbSet<T>之Include方法与IQueryable<T>扩展方法Include的使用

    Entity Framework使用Code First方式时,实体之间已经配置好关系,根据实际情况某些情况下需要同时获取导航属性,比如获取商品的同时需要获取分类属性(导航属性),或者基于优化方面考虑 ...

  3. java.util.ResourceBundle学习笔记

    一.初次使用,从网上查的资料,知识点参考JDK API和博文http://lavasoft.blog.51cto.com/62575/184605(该博主写的清晰易懂) 二.自己在程序中的具体应用: ...

  4. stm32中断遵循原则

    故障案例: 定时器定时触发一个定时事件,在这个事件里面,会调用一个串口发送程序,发现串口发送数据不完整. 分析: 1.将发送函数剥离,放到独立的线程工作,运行稳定 2.使用单步调试,在定时中断事件中多 ...

  5. 短信文本pdu模式解析

    来源于互联网 年代较长 如有侵犯 请联系删除 text模式主要发送字符集(有限的),不能用来发送中文,但是hex moder可以发送所有字符. pdu moder被所有手机支持,主要分为7bit 8b ...

  6. dos基本指令

    目录操作 dir  操作磁盘文件目录 md / mkdir <folder name> 创建子目录make directory cd  改变当前工作目录,返回上一级 cd .. rd  删 ...

  7. 1 byte 8 bit 1 sh 1 bit 2. 字符与编码在程序中的实现

    https://en.wikipedia.org/wiki/Shannon_(unit) 1字节(英语:Byte)=8比特(英语:bit) The shannon (symbol Sh), also ...

  8. python immutable and mutable

    https://blog.csdn.net/hsuxu/article/details/7785835 python mutable as default parameter(NONONO) def ...

  9. RENAME方法进行分区改造

    1.新建零时表 set time on timing on create table RPTUSER.RPT_TMP_10086BDL_bak ( HANDLEDATE DATE, TEL_CALLI ...

  10. caffe编译报错解决

    添加ssd中的一些层之后,编译报错: ../lib/libcaffe.so.1.0.0-rc5:对‘boost::match_results<__gnu_cxx::__normal_iterat ...