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