Data Files: reading and writing text files Data Files: การอ่านและเขียนไฟล์ข้อความ

Learn open / write / read / append / readline / readlines / splitlines and the context manager (with) เรียนรู้ open / write / read / append / readline / readlines / splitlines และ Context Manager (with)

Programs often need to save or load text from a file, not just the keyboard and screen as in Input and Output. In Python you open a file with open(filename, mode) — mode 'w' to write fresh, 'r' to read, and 'a' to append — then work with the file and close it with close(). It is best to use with open(...) as f: (a context manager), because it closes the file for you automatically when the block ends.

โปรแกรมมักต้องเก็บหรือโหลดข้อความจากไฟล์ ไม่ใช่แค่คีย์บอร์ดกับจออย่างในบท Input and Output ใน Python เปิดไฟล์ด้วย open(ชื่อไฟล์, โหมด) — โหมด 'w' สำหรับเขียนใหม่, 'r' สำหรับอ่าน และ 'a' สำหรับต่อท้าย (append) — จากนั้นทำงานกับไฟล์แล้วปิดด้วย close() แนะนำใช้ with open(...) as f: (Context Manager) เพราะเมื่อจบบล็อกจะปิดไฟล์ให้อัตโนมัติ

Ways to read content from a file วิธีอ่านเนื้อหาจากไฟล์

  • read() — reads the whole file as one string
  • readline() — reads one line (usually keeps the \n)
  • readlines() — reads every line into a list (each usually keeps its \n)
  • read().splitlines() — gives a list with the trailing \n removed
  • read() — อ่านทั้งไฟล์เป็นสตริงเดียว
  • readline() — อ่านหนึ่งบรรทัด (มักรวม \n)
  • readlines() — อ่านทุกบรรทัดเป็น list (แต่ละตัวมักมี \n)
  • read().splitlines() — ได้ list โดยตัด \n ออกจากท้ายบรรทัด

1 Write text to a file with open / write / close 1 เขียนข้อความลงไฟล์ด้วย open / write / close

Use open(path, 'w') to open a file for writing, then f.write(...) writes a string to it. A \n in the string means a new line, and you must call f.close() when done. After a successful Run there is a download link for the .txt file written.

ใช้ open(path, 'w') เพื่อเปิดไฟล์สำหรับเขียน จากนั้น f.write(...) เขียนสตริงลงไฟล์ ตัวอักษร \n ในสตริงหมายถึงขึ้นบรรทัดใหม่ และต้องเรียก f.close() เมื่อใช้เสร็จ หลังกด Run สำเร็จ จะมีลิงก์ดาวน์โหลดไฟล์ .txt ที่เขียนไว้

[1]
[output]
wrote file.txt

2 Write with a context manager (with) 2 เขียนด้วย Context Manager (with)

Use with open(...) as f: as a context manager — when the indented block ends, Python calls close() for you. So you do not write f.close() yourself, and you are less likely to forget to close the file. After a successful Run you can download file2.txt.

ใช้ with open(...) as f: เป็น Context Manager — เมื่อจบบล็อกย่อหน้า Python จะเรียก close() ให้อัตโนมัติ จึงไม่ต้องเขียน f.close() เอง และลดโอกาสลืมปิดไฟล์ หลังกด Run สำเร็จ สามารถดาวน์โหลด file2.txt ได้

[2]
[output]
wrote file2.txt

3 Read the whole file with read() 3 อ่านทั้งไฟล์ด้วย read()

Use open(..., 'r') to open for reading and f.read() to read all the content as one string. You may optionally pick a .txt file before Run — it is read by JavaScript in the browser (never uploaded) and used as file.txt.

ใช้ open(..., 'r') เพื่อเปิดอ่าน และ f.read() เพื่ออ่านทั้งเนื้อหาเป็นสตริงเดียว เลือกไฟล์ .txt จากเครื่องก่อนกด Run — ระบบอ่านด้วย JavaScript ในเบราว์เซอร์ (ไม่เก็บขึ้นเซิร์ฟเวอร์) แล้วใช้เนื้อหานั้นเป็น file.txt

[3]
[output]
Line 1
Line 2

Note: the code above simulates Python syntax (open / read etc.). Run works through JavaScript in the browser; when you upload a .txt, its content is used as file.txt (the write in the example is demo syntax and will not overwrite the file you chose) — nothing is sent to a server.

หมายเหตุ: โค้ดด้านบนเป็นการจำลองไวยากรณ์ Python (open / read ฯลฯ) การกด Run ทำงานผ่าน JavaScript ในเบราว์เซอร์ เมื่ออัปโหลด .txt แล้ว ระบบจะใช้เนื้อหาที่อ่านได้เป็น file.txt (ส่วน write ในตัวอย่างเป็นไวยากรณ์สาธิต จะไม่ทับไฟล์ที่เลือก) — ไม่ส่งไฟล์ไปเซิร์ฟเวอร์

4 Read the first line with readline() 4 อ่านบรรทัดแรกด้วย readline()

Use readline() to read one line from the file's current position. The result usually keeps the newline \n at the end (if the file has one). Optionally choose a .txt file before Run.

ใช้ readline() เพื่ออ่านหนึ่งบรรทัดจากตำแหน่งปัจจุบันของไฟล์ ผลลัพธ์มักรวมเครื่องหมายขึ้นบรรทัดใหม่ \n ไว้ท้ายสตริงด้วย (ถ้ามีในไฟล์) เลือกไฟล์ .txt ก่อนกด Run

[4]
[output]
Line 1

Note: Python syntax simulated with JavaScript in the browser; a .txt file is read with FileReader and never uploaded.

หมายเหตุ: จำลองไวยากรณ์ Python ด้วย JavaScript ในเบราว์เซอร์ อ่านไฟล์ .txt ด้วย FileReader ไม่เก็บขึ้นเซิร์ฟเวอร์

5 Read all lines into a list with readlines() 5 อ่านทุกบรรทัดเป็น list ด้วย readlines()

Use readlines() to read every line and return a list of strings. Each item usually keeps its trailing \n — reach one by index, e.g. lines[0]. Without a mode, open('file.txt') opens for reading ('r') by default.

ใช้ readlines() เพื่ออ่านทุกบรรทัดแล้วคืนค่าเป็น list ของสตริง แต่ละสมาชิกมักมี \n ท้ายบรรทัด — เข้าถึงด้วยดัชนีเช่น lines[0] ถ้าไม่ระบุโหมด open('file.txt') จะเปิดแบบอ่าน ('r') เป็นค่าเริ่มต้น

[5]
[output]
['Line 1\n', 'Line 2\n']
index 0: Line 1
index 1: Line 2

Note: Python syntax simulated with JavaScript in the browser; a .txt file is read with FileReader and never uploaded.

หมายเหตุ: จำลองไวยากรณ์ Python ด้วย JavaScript ในเบราว์เซอร์ อ่านไฟล์ .txt ด้วย FileReader ไม่เก็บขึ้นเซิร์ฟเวอร์

6 Read into a list without newlines using splitlines() 6 อ่านเป็น list โดยตัด newline ด้วย splitlines()

Use f.read().splitlines() to read the whole file and split it into a list of lines. The string method splitlines() removes the trailing \n from each line, unlike readlines(), which usually keeps it.

ใช้ f.read().splitlines() เพื่ออ่านทั้งไฟล์แล้วแยกเป็น list ของบรรทัด โดยเมธอด splitlines() ของสตริงจะตัด \n ออกจากท้ายแต่ละบรรทัด ต่างจาก readlines() ที่มักยังเก็บ \n ไว้

[6]
[output]
['Line 1', 'Line 2']
index 0: Line 1
index 1: Line 2

Note: Python syntax simulated with JavaScript in the browser; a .txt file is read with FileReader and never uploaded.

หมายเหตุ: จำลองไวยากรณ์ Python ด้วย JavaScript ในเบราว์เซอร์ อ่านไฟล์ .txt ด้วย FileReader ไม่เก็บขึ้นเซิร์ฟเวอร์

7 Add line numbers — write numbered lines to a new file 7 Add Line Numbers — ใส่เลขบรรทัดลงไฟล์ใหม่

Read a source file with readlines(), then write a new file in the form number: text using a for loop and a counter n starting at 1. After Run you can download notes_numbered.txt.

อ่านไฟล์ต้นทางด้วย readlines() แล้วเขียนไฟล์ใหม่ในรูปแบบ เลข: ข้อความ โดยใช้ลูป for และตัวนับ n เริ่มที่ 1 หลัง Run สามารถดาวน์โหลด notes_numbered.txt ได้

[7]
[output]
1: Hello
2: World
3: Python

Practice — from the "add line numbers" file แบบฝึก — จากไฟล์ใส่เลขบรรทัด

Suppose notes.txt has these 3 lines (no upload needed):

สมมติไฟล์ notes.txt มีเนื้อหา 3 บรรทัดดังนี้ (ไม่ต้องอัปโหลดไฟล์):

  • Line 1: Hello
  • Line 2: World
  • Line 3: Python
  • บรรทัดที่ 1: Hello
  • บรรทัดที่ 2: World
  • บรรทัดที่ 3: Python
Hello
World
Python

The idea of the program:

แนวคิดของโปรแกรม:

with open('notes.txt', 'r') as src:
    lines = src.readlines()
with open('notes_numbered.txt', 'w') as out:
    n = 1
    for line in lines:
        out.write(f'{n}: {line}')
        n = n + 1

Q1: From the file above (line 1 = Hello, line 2 = World, line 3 = Python), what should line 2 of the output file notes_numbered.txt be? ข้อ 1: จากไฟล์ด้านบน (บรรทัดที่ 1 = Hello, บรรทัดที่ 2 = World, บรรทัดที่ 3 = Python) บรรทัดที่ 2 ของไฟล์ผลลัพธ์ notes_numbered.txt ควรเป็นอะไร?

Q2: From the same file, if you call lines = src.readlines(), what is lines[0] roughly? ข้อ 2: จากไฟล์เดียวกัน ถ้าเรียก lines = src.readlines() ค่าประมาณของ lines[0] คืออะไร?

8 Append — add data to the end of a file 8 Append — ต่อท้ายข้อมูลในไฟล์

Mode 'a' (append) opens a file to write at the end without erasing the existing content, unlike 'w', which starts writing from an empty file.

โหมด 'a' (append) เปิดไฟล์เพื่อเขียนต่อท้ายโดยไม่ลบเนื้อหาเดิม ต่างจาก 'w' ที่เริ่มเขียนใหม่จากไฟล์ว่าง

[8]
[output]
start
more

9 Reverse lines — reverse the line order in a file 9 Reverse Lines — กลับลำดับบรรทัดในไฟล์

Read the content of input.txt as a list of lines, then write it in reverse order to a new file output.txt. Use the slice lines[::-1] to reverse the list.

อ่านเนื้อหาจาก input.txt เป็น list ของบรรทัด แล้วเขียนกลับลำดับไปยังไฟล์ใหม่ output.txt ใช้สไลซ์ lines[::-1] เพื่อกลับลำดับ list

[9]
[output]
C
B
A

Note: Run on this page is a JavaScript simulation, not real Python. The reading examples (Demos 3–6) read a .txt only with JavaScript in the browser, never uploaded. The write / append / reverse examples can be downloaded as a .txt (Blob) after a successful Run. For real Python, try Google Colab. หมายเหตุ: การกด Run บนหน้านี้เป็นการจำลองด้วย JavaScript ไม่ใช่ Python จริง ตัวอย่างอ่านไฟล์ (Demo 3–6) อ่าน .txt ด้วย JavaScript ในเบราว์เซอร์เท่านั้น ไม่เก็บขึ้นเซิร์ฟเวอร์ ตัวอย่างเขียน / append / reverse หลัง Run สำเร็จสามารถดาวน์โหลดเป็นไฟล์ .txt (Blob) ได้ หากต้องการทดลอง Python จริง แนะนำ Google Colab

References References / แหล่งอ้างอิง

Sources used for this lesson:

แหล่งที่ใช้ประกอบเนื้อหาในบทนี้:

  1. Reading and Writing Files — Tutorial — Reading and Writing Files (open, modes, and the basic methods) (open, โหมด, และเมธอดพื้นฐาน)
  2. open() — Built-in Functions — open (opening a file, incl. modes r / w / a) (เปิดไฟล์ รวมโหมด r / w / a)
  3. File object methods — io.TextIOBase (read / readline / readlines / write)
  4. str.splitlines() — Built-in Types — splitlines (split lines, removing the newline) (แยกบรรทัดโดยตัด newline)
  5. The with statement — Compound statements — with (Context Manager)

End-of-lesson quizแบบฝึกหัดท้ายบท

ตอบแล้ว 0 / 10

ข้อ 1

1 / 10

Which call opens a file to write fresh text?คำสั่งใดเปิดไฟล์เพื่อเขียนข้อความใหม่?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (B) open('a.txt', 'w')open('a.txt', 'w')

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) open('a.txt', 'r')open('a.txt', 'r')
    Wrong: mode 'r' opens for reading, not writing.ผิด: โหมด 'r' คือเปิดเพื่ออ่าน ไม่ใช่เขียน
  • (B) open('a.txt', 'w')open('a.txt', 'w')
    Correct! Mode 'w' opens a file for writing, as taught in this lesson.ถูกต้อง! โหมด 'w' ใช้เปิดไฟล์เพื่อเขียน ตามที่สอนในบทนี้
  • (C) print('a.txt')print('a.txt')
    Wrong: print() shows output on screen; it does not open a file.ผิด: print() แสดงผลบนจอ ไม่ได้เปิดไฟล์
  • (D) input('a.txt')input('a.txt')
    Wrong: input() reads from the keyboard; it does not open a file.ผิด: input() รับค่าจากคีย์บอร์ด ไม่ได้เปิดไฟล์

ข้อ 2

2 / 10

What is the main benefit of with open(...) as f:?ข้อดีหลักของ with open(...) as f: คืออะไร?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (A) It closes the file automatically when the block endsเมื่อจบบล็อกจะปิดไฟล์ให้อัตโนมัติ

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) It closes the file automatically when the block endsเมื่อจบบล็อกจะปิดไฟล์ให้อัตโนมัติ
    Correct! The context manager calls close() for you when the with block ends.ถูกต้อง! Context Manager จะเรียก close() ให้เมื่อจบบล็อก with
  • (B) It grows the file automaticallyทำให้ไฟล์ใหญ่ขึ้นอัตโนมัติ
    Wrong: with does not grow the file by itself.ผิด: with ไม่ได้ทำให้ไฟล์ใหญ่ขึ้นเอง
  • (C) It switches mode from r to w by itselfเปลี่ยนโหมดจาก r เป็น w เอง
    Wrong: the mode is set at open, as written in the code.ผิด: โหมดต้องระบุตอน open ตามที่เขียนในโค้ด
  • (D) It can replace print()แทนที่ print() ได้
    Wrong: with manages opening/closing a file; it does not replace print().ผิด: with ใช้จัดการการเปิด/ปิดไฟล์ ไม่ได้แทน print()

ข้อ 3

3 / 10

What does f.readline() do?f.readline() ทำอะไร?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (C) Reads one lineอ่านหนึ่งบรรทัด

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) Reads the whole file into a listอ่านทั้งไฟล์เป็น list
    Wrong: reading the whole file into a list is readlines(), not readline().ผิด: อ่านทั้งไฟล์เป็น list คือ readlines() ไม่ใช่ readline()
  • (B) Writes one lineเขียนหนึ่งบรรทัด
    Wrong: writing uses write(), not readline().ผิด: การเขียนใช้ write() ไม่ใช่ readline()
  • (C) Reads one lineอ่านหนึ่งบรรทัด
    Correct! readline() reads one line from the file's current position.ถูกต้อง! readline() อ่านหนึ่งบรรทัดจากตำแหน่งปัจจุบันของไฟล์
  • (D) Deletes the fileลบไฟล์
    Wrong: readline() reads data; it does not delete the file.ผิด: readline() อ่านข้อมูล ไม่ได้ลบไฟล์

ข้อ 4

4 / 10

How does f.read().splitlines() differ from f.readlines() in this lesson?ผลของ f.read().splitlines() ต่างจาก f.readlines() อย่างไรในบทนี้?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (D) splitlines() gives a list with no trailing \nsplitlines() ได้ list โดยไม่มี \n ท้ายบรรทัด

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) No difference; they are exactly the sameไม่ได้อะไรเลย ทั้งคู่เหมือนกันทุกประการ
    Wrong: both give a list, but splitlines() removes the \n while readlines() usually keeps it.ผิด: ทั้งคู่ได้ list แต่ splitlines() ตัด \n ออก ส่วน readlines() มักยังมี \n
  • (B) splitlines() returns a single stringsplitlines() คืนสตริงเดียว
    Wrong: splitlines() returns a list of lines, not a single string.ผิด: splitlines() คืน list ของบรรทัด ไม่ใช่สตริงเดียว
  • (C) readlines() always removes the \nreadlines() ตัด \n ออกเสมอ
    Wrong: readlines() usually keeps the \n at the end of each line.ผิด: readlines() มักเก็บ \n ไว้ท้ายแต่ละบรรทัด
  • (D) splitlines() gives a list with no trailing \nsplitlines() ได้ list โดยไม่มี \n ท้ายบรรทัด
    Correct! As in the lesson, splitlines() gives a list with the newline removed.ถูกต้อง! ตามตัวอย่างในบท splitlines() ได้ list ที่ตัด newline ออกแล้ว

ข้อ 5

5 / 10

The source notes.txt has 2 lines: line 1 = Hello, line 2 = World. The program prefixes line numbers as number: text — what should line 2 of the output file be?ไฟล์ต้นทาง notes.txt มี 2 บรรทัด: บรรทัดที่ 1 = Hello, บรรทัดที่ 2 = World โปรแกรมใส่เลขบรรทัดในรูปแบบ เลข: ข้อความ — บรรทัดที่ 2 ของไฟล์ผลลัพธ์ควรเป็นอะไร?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (B) 2: World2: World

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) 0: World0: World
    Wrong: the example counts from 1, not 0.ผิด: ในตัวอย่างเริ่มนับที่ 1 ไม่ใช่ 0
  • (B) 2: World2: World
    Correct! Source line 2 is World, so it becomes 2: World.ถูกต้อง! ต้นทางบรรทัดที่ 2 เป็น World จึงได้ 2: World
  • (C) World: 2World: 2
    Wrong: the format is the number first, then the text.ผิด: รูปแบบคือ เลขก่อน แล้วตามด้วยข้อความ
  • (D) Line WorldLine World
    Wrong: that is not the format shown in the practice.ผิด: ไม่ใช่รูปแบบที่สาธิตในแบบฝึก

ข้อ 6

6 / 10

How does mode 'a' differ from 'w'?โหมด 'a' ต่างจาก 'w' อย่างไร?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (C) 'a' appends without erasing existing content, while 'w' starts from an empty file'a' เขียนต่อท้ายโดยไม่ลบเนื้อหาเดิม ส่วน 'w' เริ่มจากไฟล์ว่าง

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) 'a' is read-only'a' อ่านอย่างเดียว
    Wrong: 'a' is append (write), not read.ผิด: 'a' คือเขียนต่อท้าย ไม่ใช่อ่าน
  • (B) 'a' deletes the file before writing'a' ลบไฟล์ก่อนเขียน
    Wrong: that is closer to what 'w' does.ผิด: นั่นใกล้เคียงกับพฤติกรรมของ 'w'
  • (C) 'a' appends without erasing existing content, while 'w' starts from an empty file'a' เขียนต่อท้ายโดยไม่ลบเนื้อหาเดิม ส่วน 'w' เริ่มจากไฟล์ว่าง
    Correct! Exactly as the Append section explains.ถูกต้อง! ตามที่บทอธิบายในหัวข้อ Append
  • (D) The two modes are the sameทั้งสองโหมดเหมือนกัน
    Wrong: 'w' overwrites, 'a' appends.ผิด: 'w' เขียนทับ ส่วน 'a' ต่อท้าย

ข้อ 7

7 / 10

What does f.read() return?f.read() คืนค่าเป็นอะไร?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (D) The whole file content as one stringเนื้อหาทั้งไฟล์เป็นสตริงเดียว

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) A list of lineslist ของบรรทัด
    Wrong: a list of lines is readlines().ผิด: list ของบรรทัดคือ readlines()
  • (B) The number of linesจำนวนบรรทัด
    Wrong: read() does not count lines.ผิด: read() ไม่ได้นับบรรทัด
  • (C) One lineหนึ่งบรรทัด
    Wrong: one line is readline().ผิด: หนึ่งบรรทัดคือ readline()
  • (D) The whole file content as one stringเนื้อหาทั้งไฟล์เป็นสตริงเดียว
    Correct! read() reads the whole file as one string.ถูกต้อง! read() อ่านทั้งไฟล์เป็นสตริงเดียว

ข้อ 8

8 / 10

open('file.txt') with no mode opens in which mode?open('file.txt') โดยไม่ระบุโหมด จะเปิดแบบใด?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (A) Read ('r') by defaultอ่าน ('r') เป็นค่าเริ่มต้น

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) Read ('r') by defaultอ่าน ('r') เป็นค่าเริ่มต้น
    Correct! With no mode given, Python uses 'r'.ถูกต้อง! ถ้าไม่ระบุโหมด Python ใช้ 'r'
  • (B) Write ('w') by defaultเขียน ('w') เป็นค่าเริ่มต้น
    Wrong: the default is 'r', not 'w'.ผิด: ค่าเริ่มต้นคือ 'r' ไม่ใช่ 'w'
  • (C) Append ('a') by defaultต่อท้าย ('a') เป็นค่าเริ่มต้น
    Wrong: you must specify 'a' yourself.ผิด: ต้องระบุ 'a' เอง
  • (D) An error, because a mode is always requiredError เพราะต้องระบุโหมดเสมอ
    Wrong: the mode is optional; the default is 'r'.ผิด: โหมดไม่บังคับ ค่าเริ่มต้นคือ 'r'

ข้อ 9

9 / 10

In the Reverse Lines example, what does lines[::-1] do?ในตัวอย่าง Reverse Lines lines[::-1] ทำอะไร?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (C) Returns the list with its items in reverse orderคืน list ที่กลับลำดับสมาชิก

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) Removes all items of the listลบสมาชิกทั้งหมดของ list
    Wrong: a slice does not remove items.ผิด: slice ไม่ได้ลบสมาชิก
  • (B) Takes only the last itemเอาเฉพาะสมาชิกตัวสุดท้าย
    Wrong: that is lines[-1].ผิด: นั่นคือ lines[-1]
  • (C) Returns the list with its items in reverse orderคืน list ที่กลับลำดับสมาชิก
    Correct! Step -1 walks from the end to the front.ถูกต้อง! step -1 ทำให้ไล่จากท้ายมาหน้า
  • (D) Sorts the items from smallest to largestเรียงสมาชิกจากน้อยไปมาก
    Wrong: a slice does not sort; it just reverses the existing order.ผิด: slice ไม่จัดเรียง เพียงกลับลำดับเดิม

ข้อ 10

10 / 10

What happens with open('missing.txt', 'r') when the file does not exist?เกิดอะไรขึ้นเมื่อ open('missing.txt', 'r') แต่ไฟล์ไม่มีอยู่จริง?

เฉลย / Answerซ่อนเฉลย / Hide

เฉลย / Answer: (D) It raises a FileNotFoundErrorเกิดข้อผิดพลาด FileNotFoundError

คำอธิบายแต่ละตัวเลือก / Explanation for each choice

  • (A) It creates an empty file for youสร้างไฟล์ว่างให้เอง
    Wrong: mode 'r' does not create a new file.ผิด: โหมด 'r' ไม่สร้างไฟล์ใหม่
  • (B) It returns Noneคืนค่า None
    Wrong: a failed open raises an error; it does not return None.ผิด: การเปิดล้มเหลวจะเกิดข้อผิดพลาด ไม่ได้คืน None
  • (C) It opens some other file automaticallyเปิดไฟล์อื่นแทนโดยอัตโนมัติ
    Wrong: Python does not guess a file for you.ผิด: Python ไม่เดาไฟล์ให้
  • (D) It raises a FileNotFoundErrorเกิดข้อผิดพลาด FileNotFoundError
    Correct! Opening a missing file for reading raises FileNotFoundError.ถูกต้อง! เปิดอ่านไฟล์ที่ไม่มีอยู่จะเกิด FileNotFoundError