Computer >> บทช่วยสอนคอมพิวเตอร์ >  >> การเขียนโปรแกรม >> Python

Attributes ของ File Object ใน Python

File object มี methods และ attributes มาก รายการเต็ม: Python docs

Methods สำคัญ:

  • close() - ปิดไฟล์
  • next() - iterator ดึงบรรทัดถัดไป
  • read([size]) - อ่านสูงสุด size bytes
  • readline([size]) - อ่าน 1 บรรทัด
  • seek(offset[, whence]) - ตั้งตำแหน่ง
  • tell() - ดูตำแหน่งปัจจุบัน
  • write(str) - เขียน string
  • writelines(sequence) - เขียน sequence ของ strings

Attributes สำคัญ:

  • closed - bool สถานะเปิด/ปิด
  • encoding - encoding ที่ใช้
  • mode - I/O mode
  • name - ชื่อไฟล์ (ถ้าสร้างจาก open())