Text file = ไฟล์ที่มีเฉพาะ readable characters Binary files = ไฟล์รูป/เสียง/เอกสาร binary ต้องมีโปรแกรมพิเศษอ่าน Text file แก้ไขได้ง่าย ประมวลผลใน Python สะดวก
f = open('my_file.txt', 'r+')
my_file_data = f.read()
f.close()
อ่านทีละบรรทัด:
f = open('my_file.txt', 'r+')
for line in f.readlines():
print(line)
f.close()
เขียนทับไฟล์ (write mode):
f = open("my_file.txt", "w")
f.write("My File!")
f.close()
Append mode (เพิ่มท้ายไฟล์):
f = open("my_file.txt", "a")
f.write("Appended text")
f.close()