标签:tin test admin use win 终端 刷新 readlines mini
#Author:JR # -*- coding:utf-8 -*- # import sys , time # f = open("C:/Users/Administrator/PycharmProjects/mypy/weak three/yesterday",‘a‘, encoding="utf-8") # print(f.read()) ##注意,python中\为转义字符,windows上的\写成/或则全部 # f=open("C:/Users/Administrator/PycharmProjects/mypy/weak three/yesterday",‘a‘, encoding="utf-8") # f.write("\ntest--------------****------------\n") ##‘a‘ 追加的方式写,不会覆盖源文件 # f.close() f=open("C:/Users/Administrator/PycharmProjects/mypy/weak three/yesterday",‘r‘,encoding="utf-8") # date=f.read() # print(date) ##‘r‘模式打开的时候不可写 print(f.readlines()) # # for i in f.readlines(): # print(i.strip()) ## it‘s too low ‘‘‘ for index,line in enumerate(f.readlines()): ##所有内容一次性读取到内存中,不能读取大文件 if index == 9: print(‘---------我是分割线----------‘) continue print(line.strip()) ##进阶---------------------------------强烈建议这样用 count=0 for line in f: #迭代器,读一行,存入内存,且只保存一行 if count==9: print("分割线又来啦------------\n") count +=1 continue print(line) count +=1 for i in range(50): sys.stdout.write("#") ##标准输出到文件(终端设备) sys.stdout.flush() ##刷新缓存中的内容到文件,输出设备也是文件 time.sleep(0.1) ##睡眠0.1秒 f=open("C:/Users/Administrator/PycharmProjects/mypy/weak three/yesterday",‘r+‘,encoding="utf-8") ##追加的方式写 f.readline() #读写的方式打开文件 f.readline() f.readline() ##输出前3行 f.write("\nr+很厉害的样子哦!!!!!!!!!!!!\n") f.seek(0) ##回到文件的初始位置,相当与光标移到第一行开始 print(f.readlines()) ‘‘‘ ‘‘‘ ##实现文件的内容更改,本质上是读取不需要更改的行到新文件中,匹配到需要更新的内容时候,更新后写入新文件 f=open("C:/Users/Administrator/PycharmProjects/mypy/weak three/yesterday",‘r+‘,encoding="utf-8") f2=open("C:/Users/Administrator/PycharmProjects/mypy/weak three/yesterday.bak",‘w+‘,encoding="utf-8") ##创建一个备份文件, for line in f: if "我以嘲笑的姿态面对生活": line = line.replace("我以嘲笑的姿态面对生活", "---------呵呵---------") f2.write(line) ‘‘‘
标签:tin test admin use win 终端 刷新 readlines mini
原文地址:http://www.cnblogs.com/jhon-018/p/7142027.html