נבנה על ידיBuilt by Ariel Yohimovich
open(שם, "w", encoding="utf-8")open(name, "w", encoding="utf-8")f.write(טקסט) לא מוסיף ירידת שורהf.write(text) adds no newline"w" מוחק את התוכן הקודם"w" erases the old contentopen פותח קובץ, ו-with דואג לסגור אותו בסוף. תמיד מוסיפים encoding="utf-8" כדי שעברית תישמר נכון.Until now all data vanished when the program ended. A file keeps it on disk. open opens a file, and with makes sure it closes at the end. Always add encoding="utf-8" so Hebrew is saved correctly.f.read() הכול כמחרוזת אחתf.read() all as one stringfor line in f: שורה אחרי שורהfor line in f: line by line.strip() מסיר את ה-\n.strip() removes the \nfor עליו: כל סיבוב נותן שורה אחת, כולל ה-\n שבסופה, ולכן מנקים עם strip. לקבצים קטנים read() מחזירה את כל התוכן.The handy way to read a file is a for loop over it: each pass gives one line, including its trailing \n, so clean it with strip. For small files read() returns everything."r" קריאה (ברירת מחדל)"r" read (default)"w" כתיבה מאפס"w" write from scratch"a" הוספה לסוף"a" append to the end"w" מוחק את כל מה שהיה בקובץ ברגע הפתיחה. כשרוצים להוסיף שורה ליומן, משתמשים ב-"a".The mode decides what you do with the file. Most dangerous: "w" erases everything the moment it opens. To add a line to a log, use "a"."w" הוא לקרוע את הדף ולהתחיל דף חדש. "a" הוא להמשיך לכתוב בשורה הבאה של אותו דף."w" is tearing out the page and starting fresh. "a" is carrying on writing on the next line of the same page.with סוגר את הקובץ לבדwith closes the file for youf.close(), ואם שגיאה קורית לפני כן, הקובץ נשאר פתוח ולפעמים חלק מהכתיבה לא נשמר.with opens a resource and closes it when the block ends, no matter what. Without with you must remember f.close(), and if an error happens first the file stays open and some writing may not be saved.Path("data") / "books.txt"Path("data") / "books.txt".exists(), .mkdir(), .read_text().exists(), .mkdir(), .read_text()/ מחבר חלקי נתיב בלי לדאוג ל-\ של Windows או / של Mac. יש לו קיצורים כמו read_text ו-write_text לקבצים קטנים.pathlib represents paths as objects. The / operator joins path parts without worrying about Windows \ or Mac /. It has shortcuts like read_text and write_text for small files.from pathlib import Path
Path("data").mkdir()
Path("data").mkdir()FileExistsError: [Errno 17] File exists: 'data'
FileExistsError: מוסיפים exist_ok=True, ואז זה בסדר גם אם התיקייה קיימת.FileExistsError: add exist_ok=True, then it's fine if the folder exists.encoding="utf-8"Always encoding="utf-8"open בלי encoding עלול לבחור קידוד ישן, ואז עברית נשמרת כג'יבריש או נופלת בשגיאה. לכן תמיד כותבים encoding="utf-8" במפורש.An encoding decides how characters are stored as bytes. UTF-8 supports every language. On Windows, open without an encoding may pick an old one, so Hebrew gets saved as gibberish or crashes. So always write encoding="utf-8" explicitly.text = "שלום 📚"
data = text.encode("utf-8")
print(len(text), "תווים")
print(len(data), "בתים")
print(data.decode("utf-8"))text = "שלום 📚"
data = text.encode("utf-8")
print(len(text), "characters")
print(len(data), "bytes")
print(data.decode("utf-8"))text = "שלום 📚"
data = text.encode("utf-8")
print(len(text), "תווים")
print(len(data), "בתים")
print(data.decode("utf-8"))text = "שלום 📚"
data = text.encode("utf-8")
print(len(text), "characters")
print(len(data), "bytes")
print(data.decode("utf-8"))6 תווים 13 בתים שלום 📚
6 characters 13 bytes שלום 📚
open(שם, "w", encoding="utf-8")open(name, "w", encoding="utf-8")f.write(טקסט) לא מוסיף ירידת שורהf.write(text) adds no newline"w" מוחק את התוכן הקודם"w" erases the old contentf.read() הכול כמחרוזת אחתf.read() all as one stringfor line in f: שורה אחרי שורהfor line in f: line by line.strip() מסיר את ה-\n.strip() removes the \n"r" קריאה (ברירת מחדל)"r" read (default)"w" כתיבה מאפס"w" write from scratch"a" הוספה לסוף"a" append to the endwith סוגר את הקובץ לבדwith closes the file for youPath("data") / "books.txt"Path("data") / "books.txt".exists(), .mkdir(), .read_text().exists(), .mkdir(), .read_text()open(שם, "w", encoding="utf-8")open(name, "w", encoding="utf-8")f.read() הכול כמחרוזת אחתf.read() all as one string"r" קריאה (ברירת מחדל)"r" read (default)with סוגר את הקובץ לבדwith closes the file for youPath("data") / "books.txt"Path("data") / "books.txt"encoding="utf-8"Always encoding="utf-8"נבנה על ידיBuilt by Ariel Yohimovich