python基础--文件操作

'''
UnicodeDecodeError: 'gbk' codec can't decode byte 0x80 in position 106: illegal multibyte sequence
encoding="utf-8"
windows默认是使用gbk

'''
#data=open("chaochao",encoding="utf-8").read()
'''
f=open("chaochao",'r',encoding="utf-8") #赋值给f文件句柄:包含文件名、字符集、大小、和在内存中的位置 只读
d=open("chaochao",'w',encoding="utf-8") #赋值给f文件句柄:包含文件名、字符集、大小、和在内存中的位置 只新写(覆盖)
e=open("baobao",'a',encoding="utf-8") #赋值给f文件句柄:包含文件名、字符集、大小、和在内存中的位置 追加


data=f.read()
data2=f.read()

print(data)
print("------------>data-------------------------------------------------%s-------" %data2)

print(data)
d.write("我爱北京天安门,我爱宝宝,\n")
e.write("河南人在上海")
f.close()
d.close()
e.close()
'''
# #high
# f=open("chaochao",'r',encoding="utf-8")
# count=0
# i=0
# print(f.tell()) # tell按照字符计数
# f.seek(140)
# print(f.readline())
# print(f.tell()) # tell按照字符计数
# print(f.readline())
# print(f.tell()) # tell按照字符计数
# print(f.encoding) #打印文件编码
# print(f.fileno()) #返回句柄在文件系统中的编号
# # *able是判断是否可以操作对应的功能
# print(f.flush()) #默认缓存达到指定大小写入内存 强制写入内存


#
# for line in f: #打印内存是保存一行 将文本转换被计数器
# if count==9 :
# i += 1
# continue
#
# print(i,line.strip())
# count += 1

#low loop
'''
f=open("chaochao",'r',encoding="utf-8")
#for line in f.readlines():
for index,line in enumerate(f.readlines()):
if index == 9: #跳打第十行
print("---------------我是分割线--------------------")
continue
print(line.strip()) #strip 是去掉换行,和空格
# for i in range(5):
# print(f.readline())



'''

#f=open("chaochao",'r+',encoding="utf-8") #追加方式打开 读写
#f=open("chaochao",'w+',encoding="utf-8") #追加方式打开 读写
# f=open("chaochao",'rb') #以二进制形式打开
#
# print(f.readline())
# print(f.readline())
# print(f.readline())
# print("-----------------------------------woshifengexian-------------")
# print(f.readline())
# print(f.tell()) #打印光标
# #f.write("heheheheehehehehehehehehehehe--------------------------------")
# print(f.readline())
#
# f=open("chaochao",'r',encoding="utf-8")
# f_new=open("chaochao11111",'',encoding="utf-8")
#
# for line in f:
# if "hehhehehe" ==line :
# line = line.replace("hehhehehe","woshi wanyachao")
# f_new.write(line)
# f.close()
# print(f_new)
# f_new.close()
# with open("chaochao",'r',encoding="utf-8")as f,\ 可以同时打开多个文件
# open("chaochao", 'r', encoding="utf-8")as d:
# for line in f:
# if
#
posted @ 2017-12-10 23:01  baidi  阅读(145)  评论(0编辑  收藏  举报