Text files · File teks
Why files?
- Variables vanish when a program ends. A file keeps data on disk.
- Files let you save results and read them back later.
- They also hold more data than you would type by hand.
Mengapa file?
- Variabel menghilang saat program berakhir. File menyimpan data di disk.
- File memungkinkan Anda menyimpan hasil dan membacanya kembali nanti.
- Mereka juga menampung lebih banyak data daripada yang bisa Anda ketik secara manual.
Open, use, close
open(name, mode)opens a file. The mode says what you will do."r"reads,"w"writes a fresh file (replacing any old text),"a"adds to the end.- Every file you open must be closed when you finish.
Buka, gunakan, tutup
open(name, mode)membuka sebuah file. Mode menunjukkan apa yang akan Anda lakukan."r"membaca,"w"menulis file baru (mengganti teks lama),"a"menambah ke akhir.- Setiap file yang Anda buka harus ditutup setelah selesai.
The with block
with open(...) as f:opens the file and closes it for you.- All the work goes in the indented block.
- This is the safest way — the file is never left open.
Blok with
with open(...) as f:membuka file dan menutupnya untuk Anda.- Semua pekerjaan dilakukan di dalam blok yang terindentasi.
- Ini adalah cara paling aman — file tidak pernah dibiarkan terbuka.
with open("greeting.txt", "w") as f:
f.write("Hi there\n")
with open("greeting.txt", "r") as f:
print(f.read())
Reading the lines
f.read()returns the whole file as one string.for line in f:reads it one line at a time.- Each line keeps its newline, so use
line.strip()to remove it.
Membaca baris-baris
f.read()mengembalikan seluruh file sebagai satu string.for line in f:membacanya satu baris pada satu waktu.- Setiap baris tetap memiliki newline-nya, jadi gunakan
line.strip()untuk menghapusnya.
with open("fruit.txt", "w") as f:
f.write("apple\npear\n")
with open("fruit.txt", "r") as f:
for line in f:
print(line.strip())
In Cambridge pseudocode
- The exam uses
OPENFILE,WRITEFILE,READFILE, andCLOSEFILE.
Dalam pseudocode Cambridge
- Ujian menggunakan
OPENFILE,WRITEFILE,READFILE, danCLOSEFILE.
OPENFILE "notes.txt" FOR WRITE
WRITEFILE "notes.txt", "Hello"
CLOSEFILE "notes.txt"
OPENFILE "notes.txt" FOR READ
READFILE "notes.txt", line
CLOSEFILE "notes.txt"
OUTPUT line
Common mistakes
- Use
with open(...) as f:so the file closes itself. "w"erases the file first;"a"adds to the end.
Kesalahan umum
- Gunakan
with open(...) as f:agar file menutup dirinya sendiri. "w"menghapus file terlebih dahulu;"a"menambah ke akhir.
Now you try
- This playground keeps files between runs, so write the file first, then read it in the same task.
- Press Check answer to test your code.
Sekarang Anda coba
- Playground ini mempertahankan file antar run, jadi tuliskan file terlebih dahulu, lalu baca dalam tugas yang sama.
- Tekan Periksa jawaban untuk menguji kode Anda.
Reading a file · Membaca file
A file is used in three steps: open, read, close. · Sebuah file digunakan dalam tiga langkah: buka, baca, tutup.
In one run: write the two lines Hello and World to a file called notes.txt, then open it again, read it, and print what you read. · Dalam satu eksekusi: tulis dua baris Hello dan World ke dalam file bernama notes.txt, lalu buka lagi, bacalah, dan print apa yang Anda baca.
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.
Write three lines to a file, then read it back and count the lines with a loop. Store the count in count (the answer is 3). · Tulis tiga baris ke dalam file, lalu bacalah kembali dan hitung jumlah barisnya dengan perulangan. Simpan jumlah tersebut di count (jawabannya adalah 3).
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.
Write the numbers 10, 20, 30 to a file, one per line. Then read them back, convert each line with int(...), and add them up in total (the answer is 60). · Tulis angka 10, 20, 30 ke dalam file, satu per baris. Kemudian bacalah kembali, ubah setiap baris dengan int(...), dan jumlahkan di total (jawabannya adalah 60).
Click Run to see the output here. · Klik Jalankan untuk melihat output di sini.