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