قراءة الملفات النصية إلى Python
افتح مستندًا وحمّل محتواه
قراءة الملفات النصية إلى Python درس مجاني في NLP Academy على CoddyKit. هذا هو الدرس 2 من أصل 4. يمكنك قراءة الدرس كاملاً أدناه مجاناً — ثم تمرن عليه مباشرة في المتصفح باستخدام محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7. هذا الدرس جزء من مسار التعلم في NLP Academy، وتقدمك يتزامن عبر الويب وتطبيق CoddyKit. تتضمن دورة NLP Academy 4 دروس في المجموع.
بعض أجزاء هذا الدرس لم تُترجم بعد وتظهر باللغة الإنجليزية.
Real Text Lives in Files
Most NLP work starts with documents stored on disk. Your first job is to load that text from a file into a Python string. 📄
Open a File With open
The open function connects Python to a file. You give it a path and a mode like r, which means read.
f = open("notes.txt", "r")
text = f.read()
f.close()Read the Whole File
The read method pulls the entire file into one string. That is handy for small documents you want to process all at once.
content = f.read()
print(len(content)) # total charactersAlways Close What You Open
An open file holds a system resource. Forgetting to close it can lose data or leak handles, so closing matters.
The with Statement Is Safer
A with block opens the file and closes it automatically when you are done, even if an error happens partway through.
with open("notes.txt", "r") as f:
text = f.read()Always Set the Encoding
Pass encoding so Python decodes bytes correctly. Using utf-8 explicitly avoids surprises across different machines.
with open("notes.txt", encoding="utf-8") as f:
text = f.read()Read Line by Line
Looping over the file object yields one line at a time. This is gentle on memory for very large documents.
with open("notes.txt", encoding="utf-8") as f:
for line in f:
print(line.strip())Get All Lines as a List
The readlines method returns every line as a list of strings, which is useful when you want to index or count lines.
lines = f.readlines()
print(len(lines)) # number of linesLines Keep Their Newline
Each read line ends with a hidden newline character. Call strip to remove that trailing whitespace before processing.
clean = line.strip()Handle Missing Files
If the path is wrong, Python raises a FileNotFoundError. Wrap risky reads in try and except to fail gracefully.
try:
open("missing.txt")
except FileNotFoundError:
print("No such file")Paths Matter
A relative path is read from where you run the script. When in doubt, use a full path so Python finds the right file.
Quick Check
Think about why one way of opening files is preferred.
Recap: Loading Text Safely
You learned to open files with a with block, set utf-8 encoding, read whole text or line by line, and handle missing files. 📥
الأسئلة الشائعة
هل درس «قراءة الملفات النصية إلى Python» مجاني؟
نعم — نص درس «قراءة الملفات النصية إلى Python» كامل متاح مجاناً هنا على الويب. لتمرينه بشكل تفاعلي (محرر أكواد مدمج ومدرس ذكاء اصطناعي متاح 24/7) وفتح باقي دورة NLP Academy، انتقل إلى CoddyKit PRO. تتضمن دورة NLP Academy 4 دروس في المجموع.
ماذا ستتعلم في «قراءة الملفات النصية إلى Python»؟
افتح مستندًا وحمّل محتواه تتمرن على NLP Academy مع أكواد عملية تشغلها مباشرة في المتصفح، ومدرس ذكاء اصطناعي متاح 24/7 يجيب على أسئلتك أثناء عملك.
هل أحتاج إلى خبرة سابقة لأبدأ NLP Academy؟
لا تُشترط خبرة سابقة. NLP Academy على CoddyKit منظم للمبتدئين حتى المتقدمين، لذا يمكنك البدء من هنا أو من البداية والتقدم بسرعتك الخاصة. هذا هو الدرس 2 من أصل 4.
كم من الوقت يستغرق درس «قراءة الملفات النصية إلى Python»؟
معظم دروس CoddyKit تستغرق حوالي 5–10 دقائق. كل منها موجز وتفاعلي، لذا تحرز تقدماً مستمراً وتستأنف من حيث توقفت عبر الويب والتطبيق.
هل يمكنني كتابة وتشغيل أكواد في درس NLP Academy هذا؟
نعم. كل درس في NLP Academy يتضمن محرر أكواد مدمج، لذا تكتب وتشغل أكواداً حقيقية مباشرة في متصفحك وتحصل على تعليقات فورية من الذكاء الاصطناعي — بدون إعداد محلي.
جميع الدروس في هذه الدورة
- السلاسل والمحارف والترميزات
- قراءة الملفات النصية إلى Python
- عدّ الكلمات والمحارف
- إنشاء أول جدول لتكرار الكلمات