python读取文件read()一直读取不进去


# code:utf-8

if __name__ == '__main__':

f1 = open('d:/englishbook/Tech2.txt', 'a+', encoding='UTF-8')
with open('d:/englishbook/Tech10.txt', 'a+', encoding='UTF-8') as file1:
contents = file1.read()
print(contents)
f1.write(contents)

Tech2.txt一直显示0kB

经过尝试发现是需要加一句file1.seek(0),即
# code:utf-8

if __name__ == '__main__':

f1 = open('d:/englishbook/Tech2.txt', 'a+', encoding='UTF-8')
with open('d:/englishbook/Tech10.txt', 'a+', encoding='UTF-8') as file1:
file1.seek(0)
contents = file1.read()
print(contents)
f1.write(contents)


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM