פייתון/פייתון גרסה 3/רשימת פונקציות מערכת built-in/פונקציות קלט ופלט של מסמכים

מתוך ויקיספר, אוסף הספרים והמדריכים החופשי

הדוגמה תהיה עם המסמך בעל הכיתוב:

Otto I, traditionally known as Otto the Great, was German king .
He was the oldest son of Henry I the Fowler and Matilda.


פונקציות סוג הסבר דוגמה
פונקצית open() פונקציות קלט ופלט של מסמכים פונקצית הקוראת לפייתון לקרוא נתונים ממסך או לכתוב בו
פונקצית close() פונקציות קלט ופלט של מסמכים פונקצית הקוראת לפייתון להפסיק לקרוא נתונים ממסך או לכתוב בו
פונקצית closed() פונקציות קלט ופלט של מסמכים פונקצית המחזירה ערך בוליאני בהתאם למצבה של פונקצית Open: האם היא נסגרה או נפתחה
with open('otto.txt','r+') as f:
    print(f.closed)

>>>True
פונקצית with open פונקציות קלט ופלט של מסמכים פונקצית הקוראת לפייתון לקרוא נתונים ממסך או לכתוב בו
read() פונקציות קלט ופלט של מסמכים פונקציה המאפשרת קריאה של תווים מתוך מסמך. בכל קריאה "תו הקריאה והכתיבה" (iterator) מתקדם עם הקריאה. אם נבצע קריאה אחר קריאה, יגיע בשלב מסוים התו, לסוף הטקסט, end-of-file (ובקיצור:EOF), ולכן יחזיר מחרוזת ריקה.
with open('otto.txt','r+') as f:
     print(f.read(10))
>>>   Otto I, tr
readline() פונקציות קלט ופלט של מסמכים פוקנציה הקוראת את השורה הראשונה בכל מסמך. אם נעזר בפונקציה זו על for, כל שורה תהיה ה-"i"
with open('otto.txt','r+') as f:
     print(f.readline())
   
>>>Otto I, traditionally known as Otto the Great, was German king .
list() פונקציות קלט ופלט של מסמכים פונקציה המדפיסה את כל השורות של הקובץ.
>>>with open('otto.txt','r+') as f:
     print(list(f))
     print(type(list(f)))

['Otto I, traditionally known as Otto the Great, was German king .\n', 'He was the oldest son of Henry I the Fowler and Matilda.']
<class 'list'>
readlines() פונקציות קלט ופלט של מסמכים פונקציה הקוראת את כל השורות של הטקסט. גם במקרה זה הטיפוס המתקבל הוא רשימה.
>>>with open('otto.txt','r+') as f:
     print(f.readlines())
     print(type(f.readlines()))   

['Otto I, traditionally known as Otto the Great, was German king .\n', 'He was the oldest son of Henry I the Fowler and Matilda.']
<class 'list'>


write() פונקציות קלט ופלט של מסמכים
>>>with open('otto.txt','r+') as f:
     print(f.readlines())
     f.write('text121212')

['Otto I, traditionally known as Otto the Great, was German king .\n', 'He was the oldest son of Henry I the Fowler and Matilda.']
[]

נבצע הרצאה ראשונה ושוב, הרצה נוספת:

>>>with open('otto.txt','r+') as f:
     print(f.readlines())

['Otto I, traditionally known as Otto the Great, was German king .\n', 'He was the oldest son of Henry I the Fowler and Matilda.text121212']
writelines() פונקציות קלט ופלט של מסמכים פונקציה הכותבת רשימות קצרות לתוך קובץ.
L=['text1', 'text2\n', 'text3']

with open('otto.txt','r+') as f:     
     print(f.readlines())
     f.writelines(L)

>>>['Otto I, traditionally known as Otto the Great, was German king .\n', 'He was the oldest son of Henry I the Fowler and Matilda.']
[]

with open('otto.txt','r+') as f:     
     print(f.readlines())
>>>['Otto I, traditionally known as Otto the Great, was German king .\n', 'He was the oldest son of Henry I the Fowler and Matilda.text1text2\n', 'text3']

ניתן להיעזר בפונקצית join על מנת להכניס רווח בין כל פריט ברשימה

L=['text1', 'text2', 'text3']

with open('otto.txt','r+') as f:     
     print(f.readlines())     
     f.writelines('\n'.join(L))

>>>['Otto I, traditionally known as Otto the Great, was German king .\n', 'He was the oldest son of Henry I the Fowler and Matilda']

with open('otto.txt','r+') as f:     
     print(f.readlines())     

>>>['Otto I, traditionally known as Otto the Great, was German king .\n', 'He was the oldest son of Henry I the Fowler and Matildatext1\n', 'text2\n', 'text3']
tell() פונקציות קלט ופלט של מסמכים הפונקציה מציינת למשתמש היכן מיקומו בקובץ, דהינו באיזה תו הוא נמצא.
>>>with open('otto.txt','r+') as f:
     print(f.tell())

0

כלומר המשתמש בנמצא בתו הראשון של הקובץ.

seek() פונקציות קלט ופלט של מסמכים הזזת תו "הקריאה והכתיבה" (iterator) מהמקום בו הוא נמצא אל תו אחר במחרוזת.
  1. התחלה נוכל להניע את תו הקריאה והכתיבה להתחלה על ידי בקשה להניע מקומו למיקום האפס.
  2. ניתן לבצע קפיצות בדומה לכתיבת אינדקס(על ידי הנקודתיים).
>>>with open('otto.txt','r+') as f:
     print(f.tell())
     f.seek((232))
     print(f.tell())
     f.seek(0)
     f.seek(0,2) 
     print(f.tell())
     
0
232
132

קריאה נוספת[עריכה]