文件操作with语句
import sys
data=open("gc.txt","r",encoding="utf-8") #只能读取文件data=open("gc.txt","w",encoding="utf-8") #写模式创建一个文件(如果文件名相同会覆盖掉)data=open("gc.txt","a",encoding="utf-8") #只能在文件后面追加print(data.readline()) #打印一行print(data.readlines()) #打印成列表for i in range(5):print(data.readline())'''
#low 方法 适合读小文件
for index,line in enumerate(data.readlines()):#取出下标和对应的值
if index==9:
print("----ooo----")
continue
print(line.strip())
#high bige 度一行覆盖一行,常用牛逼方法
count=0
for loo in data:
if count==4:
print('-----You will die-----')
count+=1
continue
print(loo)
count+=1
'''
#光标使用
f=open("gc.txt","r",encoding="utf-8")print(f.tell())#打印光标当前位置print(f.read(5)) #按字符读取print(f.readline()) #按行读取print(f.tell())f.seek(0)#光标移动端指定位置print(f.encoding)#打印所用字符编码print(f.flush())#强制刷新f.truncate(10)#截断,从开头开始截,光标移动后再截断没用,依然从头开始算。#f=open("yesok2",'r+',encoding="utf-8")#文件句柄 读写,在最后追加
#f=open("gc.txt",'w+',encoding="utf-8")#文件句柄 写读,覆盖原文件(创建新文件)后写。只会在最后追加。文件观后再打开写入会覆盖
#f=open("yesok2",'a+',encoding="utf-8")#文件句柄 追加读写
#f.close()
#f1.close()
#find_str=sys.argv[1]
#replace_str=sys.argv[2]
#for line in f:
#if find_str in line:
#line=line.replace(find_str,replace_str)
#f1.write(line)
#f.close()
#f1.close()
with open("gege",'r',encoding="utf-8") as f:
for line in f:
print(line) #执行完自动关闭文件
with open("gege",'r',encoding="utf-8") as f,open("gege",'r',encoding="utf-8") as f:
pass
with open("gege",'r',encoding="utf-8") as f,\
open("gege",'r',encoding="utf-8") as f2:
for line in f:
print(line) #执行完自动关闭文件
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。