Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Cách mở file read/write mode Python?


Mở file read/write, chỉ định 'w+' làm mode.

f = open('my_file.txt', 'w+')
file_content = f.read()
f.write('Hello World')
f.close()

Mở my_file.txt write mode, lưu content, ghi đè "Hello World". Cũng dùng r+ không truncate file.