f = open(my_file.txt, r+) my_file_data = f.read() f.close() อ่านทั้งไฟล์ทีเดียว ใช้ list comprehension อ่านทีละบรรทัดเก็บใน list: f = open(my_file, r+) lines = [line for line in f.readlines()] f.close()
f = open(my_file.txt, r+) my_file_data = f.read() f.close() อ่านทั้งไฟล์ทีเดียว ใช้ list comprehension อ่านทีละบรรทัดเก็บใน list: f = open(my_file, r+) lines = [line for line in f.readlines()] f.close()
Binary files = ไฟล์ที่ไม่ใช่ readable characters (JPEG, GIF, MP3, Word, PDF) Python เปิด text mode default เพิ่ม b ใน mode สำหรับ binary: ตัวอย่าง f = open(my_file.mp3, rb) file_content = f.read() f.close() เปิด binary read mode เก็บเนื้อหาใน file_content
Binary files = ไฟล์ที่ไม่ใช่ readable characters (รูป GIF, เสียง MP3, Word/PDF) เพิ่ม b ใน mode สำหรับ binary append mode ใช้ ab: ตัวอย่าง f = open(my_file.mp3, ab) file_content = f.read() f.close() เปิด binary append mode เก็บเนื้อหาใน file_content
ระบุ mode w+ สำหรับ read/write (ทับไฟล์): f = open(my_file.txt, w+) file_content = f.read() f.write(Hello World) f.close() อ่านได้ เขียนทับได้ ใช้ r+ แทนได้ (ไม่ truncate)
ระบุ mode w+b สำหรับ binary read/write (w=write, b=binary): f = open(my_file.mp3, w+b) file_content = f.read() f.write(bHello) f.close() จะเปิด binary read/write เก็บเนื้อหาใน file_content และเขียน Hello แบบ binary ทับไฟล์ สามารถใช้ r+b แทน (ไม่ truncate ไฟล์)
Python 3.0 ปล่อย Dec 2008 แก้ไขข้อบกพร่องรุ่นเก่า หลักการ: ลด feature duplication โดยลบวิธีเก่า ไม่ backward compatible โค้ด 2.x รันบน 3.x ไม่ได้ 2.7 เป็น major release สุดท้ายของ 2.x ความแตกต่างเด่น: print - keyword ใน 2.7, built-in function ใน 3.x (ต้องมีวงเล็บ) raw_input() - deprecated ใน 3.x ใช
Comment คือข้อความอธิบายโค้ด ถูก interpreter ข้าม เริ่มด้วย # จบสิ้นบรรทัด # this is a comment print(Hello World) print(Welcome to TutorialsPoint) # this is also a comment after statement Python ไม่มี multi-line comment block ต้องวาง # ทุกบรรทัด IDE หลายตัวมี shortcut comment block Triple-quoted st
Comment ใน Python เริ่มด้วย # จบที่สิ้นบรรทัด ไม่มี block comment แบบ C (/* ... */) คอมเมนต์หลายบรรทัด ให้วาง # นำหน้าทุกบรรทัด: # comment1 # comment2 # comment3 print(Hello World) Triple-quoted string ก็ถือเป็น comment ได้ ถ้าไม่ใช่ docstring: comment1 comment2 comment3 print(Hello World)
ระบุ mode a สำหรับ append: f = open(my_file.txt, a) file_content = f.read() f.write(Hello World) f.close() เพิ่ม Hello World ท้ายไฟล์
เปิดไฟล์ mode a (append) จะตั้ง pointer ที่ท้ายไฟล์: f = open(my_file.txt, a) file_content = f.read() f.write(Hello World) f.close() จะเพิ่ม Hello World ท้ายไฟล์
ระบุ mode w สำหรับ write (ทับไฟล์เดิม): f = open(my_file.txt, w) f.write(Hello World) f.close() ปลอดภัยกว่า ใช้ with (โค้ดต้นฉบับมี typo mode r ต้องเป็น w): with open(my_file.txt, w) as f: f.write(Hello World)
ระบุ mode r สำหรับ read-only: f = open(my_file.txt, r) file_content = f.read() f.close() ปลอดภัยกว่า ใช้ with: with open(my_file.txt, r) as f: print(f.read())
เปิดไฟล์ read mode เรียก readline(): f = open(my_file.txt, r) line = f.readline() print(line) f.close() ปลอดภัยกว่า ใช้ with: with open(my_file.txt, r) as f: print(f.readline())
File object มี methods และ attributes มาก รายการเต็ม: Python docs Methods สำคัญ: close() - ปิดไฟล์ next() - iterator ดึงบรรทัดถัดไป read([size]) - อ่านสูงสุด size bytes readline([size]) - อ่าน 1 บรรทัด seek(offset[, whence]) - ตั้งตำแหน่ง tell() - ดูตำแหน่งปัจจุบัน write(str) - เขียน string writeli
ใช้ seek(offset[, whence]) ตั้งตำแหน่ง whence: 0=absolute, 1=relative current, 2=relative end ตัวอย่าง: ย้ายไปก่อน l ตัวแรกใน Hello\nworld f = open(my_file, r) f.seek(2) f.close() ตำแหน่ง 2 คือหน้าตัว l ตัวแรกของ Hello
ใช้ seek(offset[, whence]) ตั้งตำแหน่งไฟล์ whence: 0=absolute (default), 1=relative to current, 2=relative to end ตัวอย่าง: กลับไปต้นไฟล์ f = open(my_file.txt, r) f.seek(0) f.close() ใช้ whence=2 ไปท้ายไฟล์ได้
ใช้ tell() ดูตำแหน่งปัจจุบันของ file object ตัวอย่าง f = open(my_file.txt, r) f.readline() print(f.tell()) f.close() ถ้าไฟล์มีข้อความ Hello\nworld หลัง readline() ตำแหน่งจะเป็น 6 (ชี้หน้า world)
Python ไร้ built-in สำหรับนี้ ต้องเขียน wrapper class เอง: class FileLineWrapper(object): def __init__(self, file): self.f = file self.curr_line = 0 def close(self): return self.f.close() def readline(self): self.curr_line += 1 return self.f.readli
ใช้ os.rename เปลี่ยนชื่อไฟล์: >>> import os >>> os.rename(a.txt, b.txt) หรือ shutil.move: ตัวอย่าง >>> import shutil >>> shutil.move(a.txt, b.txt) ถ้าไฟล์อยู่คนละโฟลเดอร์ ให้ระบุ absolute path ทั้งคู่
มี list ไฟล์เก่า-ใหม่ ใช้ os.rename: ตัวอย่าง import os for old, new in files.items(): # files.iteritems() ใน Python 2 os.rename(old, new) หรือใช้ shutil.move (ย้าย/rename ได้): ตัวอย่าง import shutil for old, new in files.items(): shutil.move(old, new)