1. 程式人生 > 其它 >Python檔案操作中 r+ 引數的用法

Python檔案操作中 r+ 引數的用法

技術標籤:Python程式設計檔案和異常

【演算法程式碼】

寫法一:

filename="C:\\test.txt"
with open(filename,'r+') as file_object:
    file_object.read()
    file_object.write("\n ...... ")

寫法二:

filename="C:\\test.txt"
with open(filename,'r+') as file_object:    
    file_object.write("\n ...... ")
    file_object.readlines()

上述兩種寫法,都可以實現往已有檔案中新增內容的功能(類似於將上面的引數'r+'改為'a')。