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.
במבחן: טיפול בקבצים
- הפסודוקוד פותח קובץ למצב מסוים, קורא או כותב, ואז סוגר אותו.
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.
טעויות נפוצות
- פתיחה, ולאחר מכן קריאה או כתיבה, ואז סגירה של הקובץ.
- שורה שנקראה עדיין מכילה את סימן הרווח החדש.
Now you try
- Write a file, then open it again to read it back.
- Press Check answer to test your code.
כעת תנסו בעצמכם
- כתוב קובץ, ואז פתח אותו שוב כדי לקרוא אותו חזרה.
- לחץ על בדוק תשובה כדי לבדוק את הקוד שלך.
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. · לחץ על הרץ כדי לראות את התוצא כאן.
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. · לחץ על הרץ כדי לראות את התוצא כאן.
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. · לחץ על הרץ כדי לראות את התוצא כאן.