Python入门:文件操作1

'''f=open("yesterday2",'a',encoding="utf-8")#文件句柄  a=append 追加,不覆盖文件 只能写不能读
f.write("我爱北京天安门 \n")
f.write("天安门上太阳升\n")
'''
# f=open("yesterday",'r',encoding='utf-8') #只读不能写
f=open("yesterday2",'w',encoding='utf-8') #只写不能读,覆盖原来的内容
f.write("\n 我爱北京天安门......")
#data=f.read()
#print(data)
f.close()


#f = open("yesterday2",'r+',encoding="utf-8") #文件句柄 读写
#f = open("yesterday2",'w+',encoding="utf-8") #文件句柄 写读
#f = open("yesterday2",'a+',encoding="utf-8") #文件句柄 追加读写
'''f = open("yesterday2",'wb') #文件句柄 二进制文件
f.write("hello binary\n".encode())
f.write("hello world\n".encode())
f.write("hello jiajia\n".encode())
f.write("hello lili\n".encode())
print(f.tell()) #目前文件标的位置
print(f.seek(0)) #返回文件头的位置
#f.close()
'''

f=open("yesterday",'r',encoding='utf-8')
#打印前9行
count=0
for line in f:
if count==9:
print('------分割线-------')
count+=1
continue
print(line.strip()) #去掉前后空格、换行符、回车符
count+=1

猜你喜欢

转载自www.cnblogs.com/luckerzhang/p/9156856.html