python以追加的方式寫文件


f=open("guru99.txt", "a+")

加號,它表示如果它不存在,它將創建一個新文件。假如已經有了文件,就不需要創建一個新的文件。寫成a就行了。

 

同時使用兩個文件

有時你可能想要讀取文件並同時寫入另一個文件。如果你使用在學習如何寫入文件時顯示的示例,它實際上可以合並到以下內容中:

d_path = 'dog_breeds.txt' d_r_path = 'dog_breeds_reversed.txt' with open(d_path, 'r') as reader, open(d_r_path, 'w') as writer: dog_breeds = reader.readlines() writer.writelines(reversed(dog_breeds))


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM