python文件操作-读和写

  示例如下:

'''一次读小文件'''
def an1_anbdj(self,event):
files=gu.fileopenbox("打开文件")
fileread=open(files,"r+")
filewrite=open("news.txt","w+")
try:
fileread.seek(0,0)
filers=fileread.read()
filewrite.write(filers)
finally:
fileread.close()
filewrite.close()

'''按行读入'''
files = gu.fileopenbox("打开文件")
fileread = open(files, "r+")
filewrite = open("news.txt", "w+")
try:
fileread.seek(0, 0)
while True:
filers = fileread.readline()
filewrite.write(filers)
if filers == "":
break
finally:
fileread.close()
filewrite.close()

'''读大文件'''
files = gu.fileopenbox("打开文件")
fileread = open(files, "r+")
filewrite = open("news.txt", "w+")
try:
while True:
filers = fileread.read(100)
filewrite.write(filers)
if filers == "":
break
finally:
fileread.close()
filewrite.close()

'''读入二进制文件'''
files = gu.fileopenbox("打开文件")
fileread = open(files, "rb")
filewrite = open("伤不起888.flv", "wb+")
try:
while True:
filers = fileread.read()
filewrite.write(filers)
if not filers:
break
finally:
fileread.close()
filewrite.close()
posted @ 2021-06-02 16:59  MMCDLONG  阅读(82)  评论(0编辑  收藏  举报