# -*-coding:UTF-8-*- #打开-操作-关闭 #模式“a” 追加 date = open("test.txt","a") date.write("chencola \n") date.write("chencola \n") date.write("chencola \n") date.close() #date1 = open("test.txt","r") #一行一行读 #print (date1.readlines()) #file句柄读行后变成一个列表,每个元素就是一行的字符 #for line in date1.readlines(): #line逐个取date.readlines()的元素,再循环把去到的line输出 # print (line.strip()) #strip把两端换行和空格去掉 #date1.close() date2 = open("test.txt","r") print date2 count = 0 for line in date2: #输出前9行 count+=1 if count>9: break else: print line.strip() print (date2.tell()) #告诉当前光标位置 date2.seek(0) #光标回到起始位置 print (date2.tell()) print date2.read(50) print date2.encoding date2.close() date3 =open("test1.txt","w") #date3.truncate() #清空或截断 #print (date3.read(200)) date3.write("chenwenhong")
python的文件操作
最新推荐文章于 2022-10-26 13:46:17 发布