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. · اضغط تشغيل لرؤية المخرجات هنا.