print("->文件句柄的获取,读操作:")
f = open('无题','r',encoding='utf8')
d = f.read()
f.close()
print(d)
print('->例二:')
f = open('无题','r',encoding='utf8')
e = f.read(9)
f.close()
print(e)
#python3中,文件中一个中英文都占位1
->文件句柄的获取,读操作: 昨夜星辰昨夜风 画楼西畔桂堂东 身无彩凤双飞翼 心有灵犀一点通 ->例二: 昨夜星辰昨夜风 画
print("写的操作,写文件的时候,不能调用读方法,读文件的时候,不能调用写方法")
f = open('python','w',encoding='utf8')
f.write("I must learn python \nbecause, python is important \n")
f.write("java is better?")
f.write("maybe") #上面的语句,没有加换行符,所以输出的内容是紧接的
f.close()
I must learn python because, python is important java is better?maybe
f = open('python','a',encoding='utf8')
f.write("花开又花落")
f.close()
I must learn python because, python is important java is better?maybe花开又花落
print("readline方法")
f = open('无题','r',encoding='utf8')
a = f.readline()
print("此时光标位置:",f.tell())
b = f.readline()
print("此时光标位置:",f.tell())
print(a.strip()) #strip是字符串方法中去除空格和换行的方法
print(b.strip())
print("readlines方法,会将每行的内容组成一个列表打印")
f = open('无题','r',encoding='utf8')
c = f.readlines()
print(c)
print(id(c))
print(id(f))
for i in c:
print(i.strip())
print("遍历方法")
f.seek(0)
for i in f:
print(i.strip())
f.close() #文件的操作中,close()方法一定不能忘记
readline方法 此时光标位置: 23 此时光标位置: 46 昨夜星辰昨夜风 画楼西畔桂堂东 readlines方法,会将每行的内容组成一个列表打印 ['昨夜星辰昨夜风\n', '画楼西畔桂堂东\n', '身无彩凤双飞翼\n', '心有灵犀一点通'] 37826824 5344280 昨夜星辰昨夜风 画楼西畔桂堂东 身无彩凤双飞翼 心有灵犀一点通 遍历方法 昨夜星辰昨夜风 画楼西畔桂堂东 身无彩凤双飞翼 心有灵犀一点通
f = open('无题','r',encoding='utf8')
f.read(4)
print('当前光标位置',f.tell())
f.seek(10)
print('当前光标位置',f.tell())
f.close()
#read时,一个中文算三个字符
import sys,time
for i in range(20):
sys.stdout.write("#")
sys.stdout.flush()
time.sleep(1)
f = open('test','w')
f.write("hello")
f.write("\n")
f.write("python")
f.flush() #这样不用执行close方法,内存中的数据,就会写入到disk
f.close()
f = open('test','a')
f.truncate(2) #截断方法,光标从2开始往后截取
f.close()
fileHandle = open ( 'test.txt', 'a' ) fileHandle.write ( '\n\nBottom line.' ) fileHandle.close()
fileHandle = open ( 'test.txt' ) print fileHandle.read() fileHandle.close()
fileHandle = open ( 'test.txt' ) print fileHandle.readline() # "This is a test." fileHandle.close()
fileHandle = open ( 'test.txt' ) fileList = fileHandle.readlines() for fileLine in fileList: print '>>', fileLine fileHandle.close()
fileHandle = open ( 'test.txt' ) print fileHandle.read ( 1 ) # "T" fileHandle.seek ( 4 ) print FileHandle.read ( 1 ) # " "(原文有错)
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-2 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2025 源码网商城 (www.ymwmall.com) 版权所有