2020-03-26
  • |
  • daafoor
  • |
  • مشاهدات: 8944

التعامل مع الملفات جزء مهم من أي تطبيق ويب.

في Python ستجد العديد من الوظائف لإنشاء الملفات وقراءتها وتحديثها وحذفها.

 التعامل مع اي ملف في بايثون يمر ب3 مراحل, 

1.فتح قناة للوصول للملف, نحدد نمطها اما قراءة او كتابة او قراءة وكتابة معا

2.معالجة الملف(القراءة منه او الكتابة فيه)

3.اغلاق القناة

ملاحظة: نمط القناة يكون واحد من هذه الخيارات:

file tube modes in python:

--------------------------

r Opens a file for reading. (default)

w Opens a file for writing. Creates a new file if it does not exist or truncates the file if it exists.

x Opens a file for exclusive creation. If the file already exists, the operation fails.

a Opens a file for appending at the end of the file without truncating it. Creates a new file if it does not exist.

t Opens in text mode. (default)

b Opens in binary mode.

+ Opens a file for updating (reading and writing)

امثلة على القراءة من الملفات

 

مثال على القراءة من ملف في بايثون:

#read a file and prints its contents

f = open("demofile.txt", "r") #1.open tube for reading
print(f.read())   #2.writing on file
f.close()   #3.close the tube

 

مثال على قراءة  جزء من ملف في بايثون:

قراءة أجزاء من الملف فقط (اول 5 محارف)

#Return the 5 first characters of the file:

f = open("demofile.txt", "r")
print(f.read(5))
f.close()

 

مثال قراءة سطر من الملف:

#Read one line of the file:

f = open("demofile.txt", "r")
print(f.readline())
f.close()

 

مثال قراءة سطرين من ملف:

#Read two lines of the file:

f = open("demofile.txt", "r")
print(f.readline())
print(f.readline())
f.close()

 

قراءة محتوى الملف بالكامل سطر سطر:

#Loop through the file line by line:

f = open("demofile.txt", "r")
for x in f:
  print(x)
f.close()

 

اكتب برنامج بلغة بايثون يقوم بانشاء ملف نصي ويكتب فيه سطرين:

#write a python program that creates a text file called myFile.txt and write on it the following lines:
#Hello
#This is me.
f = open("myFile.txt", "w")   #1.open tube for writing
f.write("Hello\nThis is me.")   #2.writing on file
f.close()   #3.close the tube

 

--------------------------------

 

امثلة على انشاء ملفات والكتابة فيها

 مثال التعديل على ملف موجود, وفي حال عدم وجوده يتم انشاؤه:

#To write to an existing file, you must add a parameter to the open() function:

#"a" - Append - will append to the end of the file

#"w" - Write - will overwrite any existing content

#Open the file "demofile2.txt" and append content to the file:


f = open("demofile2.txt", "a")
f.write("Now the file has more content!")
f.close()

#open and read the file after the appending:
f = open("demofile2.txt", "r")
print(f.read())
f.close()

 

مثال على انشاء ملف في بايثون:

#To create a new file in Python, use the open() method, with one of the following parameters:

#"x" - Create - will create a file, returns an error if the file exist

#"a" - Append - will create a file if the specified file does not exist

#"w" - Write - will create a file if the specified file does not exist

#Create a file called "myfile.txt":


f = open("myfile.txt", "x")
f.close()

 

انشاء ملف جديد في حال عدم وجوده:

#Create a new file if it does not exist:

f = open("myfile.txt", "w")
f.close()

ابحث عن مسائل برمجة بايثون | Python programming بالانجليزي

هل أعجبك المحتوى؟

محتاج مساعدة؟ تواصل مع مدرس اونلاين الان!

التعليقات
لا يوجد تعليقات
لاضافة سؤال او تعليق على المشاركة يتوجب عليك تسجيل الدخول
تسجيل الدخول