Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

วิธีเปิดไฟล์ใน Read/Write Mode ด้วย Python

ระบุ mode 'w+' สำหรับ read/write (ทับไฟล์):

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

อ่านได้ เขียนทับได้ ใช้ r+ แทนได้ (ไม่ truncate)