Text files · קובצי טקסט
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.
מדוע קבצים?
- משתנים נעלמים כאשר תוכנית נגמרת. קובץ שומר נתונים על דיסק.
- קבצים מאפשרים לשמור תוצאות ולקרוא אותן חזרה בהמשך.
- הם גם מוחזקים יותר מנתונים שתתעלם בהקלדה ידנית.
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.
פתיחה, שימוש, סגירה
open(name, mode)פותח קובץ. המצב אומר מה תעשה."r"קורא,"w"כותב קובץ חדש (מחליף כל טקסט ישן),"a"מוסיף בסוף.- כל קובץ שפותח חייב להיסגר כשנגמר.
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.
בלוק with
with open(...) as f:פותח את הקובץ וסוגר אותו בשבילך.- כל העבודה מתבצעת בבלוק הממוזז.
- זו הדרך הבטוחה ביותר — הקובץ לעולם לא נשאר פתוח.
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.
קריאת השורות
f.read()מחזיר את כל הקובץ כמחרוזת אחת.for line in f:קורא אותו שורה אחת באחת.- כל שורה שומרת על סימן הסיום שלה, אז השתמש ב
line.strip()כדי להסיר אותו.
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.
בפסאודוקוד קמברידג'
- המבחן משתמש ב
OPENFILE,WRITEFILE,READFILE, וCLOSEFILE.
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.
טעויות נפוצות
- השתמש ב
with open(...) as f:כדי שהקובץ ייסגר לבדו. "w"מנקה את הקובץ קודם;"a"מוסיף בסוף.
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.
כעת תנסו בעצמכם
- מגרש המשחק הזה שומר קבצים בין הפעלות, כך כתבו את הקובץ תחילה, ולאחר מכן קראו אותו באותו משימה.
- לחץ על בדוק תשובה כדי לבדוק את הקוד שלך.
Reading a file · קריאת קובץ
A file is used in three steps: open, read, close. · קובץ משמש בשלושה שלבים: פתוח, לקרוא, לסגור.
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. · בהפעלה אחת: כתוב את שני השורות Hello וWorld לקובץ בשם notes.txt, ואז פתח אותו שוב, קרא אותו, והדפס/הוצא print מה שקראת.
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.
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). · כתוב שלושה שורות לקובץ, ואז קרא אותן חזרה וספור את השורות בעזרת לולאה. אחסן את הספירה בcount (התשובה היא 3).
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.
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). · כתוב את המספרים 10, 20, 30 לקובץ, אחד בכל שורה. לאחר מכן קרא אותם חזרה, הפוך כל שורה עם int(...), וחבר אותם בסך בtotal (התשובה היא 60).
Click Run to see the output here. · לחץ על הרץ כדי לראות את התוצא כאן.