Text files · Tệp văn bản
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.
Tập tin lưu trữ dữ liệu
- Biến mất đi khi chương trình kết thúc; một tập tin lưu trữ dữ liệu để duy trì sự tồn tại của nó.
- Bài thi bao gồm việc đọc từ và ghi vào tập tin văn bản.
- Làm việc theo ba bước: mở, đọc hoặc ghi, sau đó đóng.
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.
Ghi vào tập tin
open(name, "w")mở tệp để ghi ("w"ghi đè lên nội dung cũ).withđóng tập tin tự động khi khối lệnh kết thúc.\nbắt đầu dòng mới trong tệp.
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.
Đọc tập tin
open(name, "r")mở tập tin để đọc..read()thu thập toàn bộ nội dung thành một chuỗi duy nhất.
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.
Đọc từng dòng một
- Duyệt qua tập tin cung cấp một dòng mỗi lần — hữu ích để cộng tổng hoặc đếm số lượng.
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.
Trong bài thi: xử lý tập tin
- Giả mã mở tập tin với chế độ, đọc hoặc ghi, sau đó đóng nó.
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.
Lỗi thường gặp
- Mở, sau đó đọc hoặc ghi, rồi đóng tập tin.
- Một dòng bạn đọc vẫn chứa ký tự xuống dòng của nó.
Now you try
- Write a file, then open it again to read it back.
- Press Check answer to test your code.
Bây giờ bạn thử
- Ghi một tập tin, sau đó mở nó trở lại để đọc lại.
- Nhấn Check answer (Kiểm tra câu trả lời) để thử mã của bạn.
Using a file · Sử dụng một tập tin
A file is used in three steps: open, read or write, close. · Một tập tin được sử dụng qua ba bước: mở, đọc hoặc ghi, đóng.
In one run: write the line Cambridge to a file called school.txt, then open it again, read it, and print what you read. · Trong một lần chạy: ghi dòng Cambridge vào một tệp có tên là school.txt, sau đó mở lại tệp đó, đọc nội dung và print những gì bạn vừa đọc.
Click Run to see the output here. · Nhấn Chạy để xem kết quả ở đây.
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). · Code mẫu viết ba dòng vào một tập tin. Đọc lại và đếm số dòng bằng vòng lặp. Lưu số lượng vào count (đáp án là 3).
Click Run to see the output here. · Nhấn Chạy để xem kết quả ở đây.
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). · Code mẫu ghi 10, 20, 30 vào một tập tin, mỗi số trên một dòng. Đọc lại, chuyển đổi mỗi dòng bằng int(...), rồi cộng dồn chúng vào total (đáp án là 60).
Click Run to see the output here. · Nhấn Chạy để xem kết quả ở đây.