Text files · ไฟล์ข้อความ (Text files)
Files keep data
- Variables vanish when a program ends; a file stores data so it lasts.
- The exam covers reading from and writing to text files.
- Work in three steps: open, read or write, then close.
ไฟล์เก็บข้อมูล
- ตัวแปรจะหายไปเมื่อโปรแกรมสิ้นสุด; ไฟล์ จะเก็บข้อมูลให้คงอยู่ได้
- การสอบครอบคลุมการอ่านและเขียน ไฟล์ข้อความ
- ทำงานสามขั้นตอน: เปิด, อ่านหรือเขียน, แล้ว ปิด
Writing to a file
open(name, "w")opens a file for writing ("w"overwrites what was there).withcloses the file automatically when the block ends.\nstarts a new line in the file.
เขียนลงในไฟล์
open(name, "w")เปิดไฟล์สำหรับ การเขียน ("w"จะทับสิ่งที่อยู่ในนั้น)withปิดไฟล์อัตโนมัติเมื่อบล็อกสิ้นสุดลง\nเริ่มต้นบรรทัดใหม่ในไฟล์
with open("notes.txt", "w") as f:
f.write("Hello\n")
f.write("World\n")
Reading a file
open(name, "r")opens a file for reading..read()pulls in the whole contents as one string.
อ่านไฟล์
open(name, "r")เปิดไฟล์เพื่อ อ่าน.read()ดึงเนื้อหาทั้งหมดเข้ามาเป็นสตริงเดียว
with open("notes.txt", "r") as f:
text = f.read()
print(text)
Reading line by line
- Looping over the file gives one line at a time — handy for totalling or counting.
อ่านบรรทัดต่อบรรทัด
- การวนลูปบนไฟล์จะให้ บรรทัดละหนึ่งบรรทัด — มีประโยชน์สำหรับการรวมผลหรือนับจำนวน
with open("notes.txt", "r") as f:
for line in f:
print(line.strip())
In the exam: file handling
- Pseudocode opens a file for a mode, reads or writes, then closes it.
ในการสอบ: การจัดการไฟล์
- Pseudocode เปิดไฟล์ด้วยโหมดหนึ่ง อ่านหรือเขียน แล้วปิด
OPENFILE "notes.txt" FOR WRITE
WRITEFILE "notes.txt", "Hello"
CLOSEFILE "notes.txt"
Common mistakes
- Open, then read or write, then close the file.
- A line you read still carries its newline character.
ข้อผิดพลาดที่พบบ่อย
- เปิด, จากนั้นอ่านหรือเขียน, แล้ว ปิด ไฟล์
- บรรทัดที่คุณอ่านยังคงมีตัวอักษร newline อยู่
Now you try
- Write a file, then open it again to read it back.
- Press Check answer to test your code.
ลองดูเลย
- เขียนไฟล์ลงมาก่อน แล้วเปิดอีกครั้งเพื่ออ่านกลับเข้าไป
- กด Check answer เพื่อทดสอบโค้ดของคุณ
Using a file · การใช้ไฟล์
A file is used in three steps: open, read or write, close. · ไฟล์ถูกใช้งานในสามขั้นตอนคือ เปิด, อ่านหรือเขียน, ปิด
In one run: write the line Cambridge to a file called school.txt, then open it again, read it, and print what you read. · ในการรันครั้งหนึ่ง: เขียนบรรทัด Cambridge ลงในไฟล์ชื่อ school.txt จากนั้นเปิดไฟล์อีกครั้ง อ่านข้อมูล และ print สิ่งที่อ่านได้
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
The starter writes three lines to a file. Read it back and count the lines with a loop. Store the count in count (the answer is 3). · โค้ดเริ่มต้นเขียนสามบรรทัดลงในไฟล์ อ่านกลับและนับจำนวนบรรทัดโดยใช้ลูป เก็บค่าที่นับได้ใน count (คำตอบคือ 3)
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่
The starter writes 10, 20, 30 to a file, one per line. Read them back, convert each line with int(...), and add them up in total (the answer is 60). · โค้ดเริ่มต้นเขียน 10, 20, 30 ลงในไฟล์ โดยแต่ละบรรทัดหนึ่งบรรทัด อ่านกลับมาแปลงแต่ละบรรทัดด้วย int(...) แล้วรวมผลไว้ใน total (คำตอบคือ 60)
Click Run to see the output here. · คลิก Run เพื่อดูผลลัพธ์ที่นี่