ใช้ readline() อ่านบรรทัดแรก (หรือบรรทัดปัจจุบัน): f = open(my_file.txt, r+) print(f.readline())
ใช้ readline() อ่านบรรทัดแรก (หรือบรรทัดปัจจุบัน): f = open(my_file.txt, r+) print(f.readline())
read() อ่านทั้งไฟล์ทีเดียว ใช้ readlines() อ่านทีละบรรทัด ตัวอย่าง f = open(my_file.txt, r+) for line in f.readlines(): print(line) f.close() แนะนำใช้ with statement ปิดอัตโนมัติ: with open(my_file.txt, r+) as f: for line in f.readlines(): print(line)
truncate([size]) ตัดขนาดไฟล์ ถ้าระบุ size จะตัดที่ size นั้น (สูงสุด) default คือ current position เรียก truncate() โดยไม่มี argument จะตัดที่ตำแหน่งปัจจุบัน ตัวอย่าง >>> # test.txt contents: ABCDE >>> f = open(test.txt, r+) >>> f.truncate(2) >>> f.read() AB หมายเ
สร้างไฟล์ขนาดเฉพาะ: seek ไปที่ byte ที่ต้องการ เขียน byte เดียว (sparse file) ตัวอย่าง: Sparse File 1 GB with open(my_file, wb) as f: f.seek(1024 * 1024 * 1024) # 1 GB f.write(b0) สร้างไฟล์เต็ม (จริงๆ ใช้พื้นที่): with open(my_file, wb) as f: num_chars = 1024 * 1024 * 1024 f.write(b
ใช้ write เขียนบรรทัดเดียว: ตัวอย่าง f = open(myfile, w) f.write(hi there\n) # Python แปลง \n เป็น os.linesep เอง f.close() # ส่วนใหญ่ละได้ เพราะ destructor จะปิดให้ หรือใช้ print() (Python 2.6+): from __future__ import print_function # Python 2 เท่านั้น print(hi there, file=f) Python 3 ไม่ต้อง i
ใช้ write คั่นบรรทัดด้วย \n: ตัวอย่าง line1 = First line line2 = Second line line3 = Third line with open(my_file.txt, w) as out: out.write({}\n{}\n{}\n.format(line1, line2, line3)) หรือใช้ writelines: ตัวอย่าง line1 = First line line2 = Second line line3 = Third line with open(my_file.txt, w) a
ใช้ os.access(path, mode) ตรวจสอบ permission ไฟล์ ตัวอย่าง >>> import os >>> os.access(my_file, os.R_OK) # read access True >>> os.access(my_file, os.W_OK) # write access True >>> os.access(my_file, os.X_OK) # execute access False >>> os.access(my_file
ใช้ os.access(path, mode) ตรวจสอบ permission ไดเรกทอรี สำหรับเขียนต้องตรวจสอบ execute permission ด้วย ตัวอย่าง >>> import os >>> os.access(my_folder, os.R_OK) # read access True >>> os.access(my_folder, os.W_OK) # write access True >>> os.access(my_folder, os.
ใช้ os.chmod(file, mode) เปลี่ยน permission mode เป็น octal (ขึ้นต้น 0o) เช่น 0o777 >>> import os >>> os.chmod(my_file, 0o777) หรือใช้ flags จากโมดูล stat หรือ subprocess.call([chmod, 0444, my_file])
บนแพลตฟอร์มที่มีคำสั่ง chmod ใช้ subprocess: >>> import subprocess >>> subprocess.call([chmod, -R, +w, my_folder]) หรือใช้ os module เขียน recursive เอง: import os def change_permissions_recursive(path, mode): for root, dirs, files in os.walk(path, topdown=False): for
ใช้ pwd, grp, os modules เปลี่ยน owner ของไฟล์/ไดเรกทอรี ตัวอย่าง import pwd import grp import os uid = pwd.getpwnam(nobody).pw_uid gid = grp.getgrnam(nogroup).gr_gid path = my_folder os.chown(path, uid, gid)
ใช้ os.chmod(file, mode) เปลี่ยน permission mode เป็น octal (ขึ้นต้น 0o) เช่น 0o777 >>> import os >>> os.chmod(my_file, 0o777) หรือใช้ flags จากโมดูล stat หรือ subprocess.call([chmod, 0444, my_file])
ใช้ os.access(path, mode) ตรวจสอบ permission และการมีอยู่ของไฟล์ ตัวอย่าง >>> import os >>> os.access(my_file, os.F_OK) # ตรวจสอบการมีอยู่ True >>> os.access(my_file, os.R_OK) # ตรวจสอบอ่านได้ True >>> os.access(my_file, os.W_OK) # ตรวจสอบเขียนได้ True >>
เหมือนบทความก่อนหน้า: อ่านไฟล์แต่ละไฟล์ เขียนต่อในไฟล์ปลายทาง ตัวอย่าง (อ่านทั้งไฟล์) filenames = [file1.txt, file2.txt, file3.txt] with open(output_file, w) as outfile: for fname in filenames: with open(fname) as infile: outfile.write(infile.read()) ตัวอย่าง (อ่านทีละบรรทัด
รวมหลายไฟล์เป็นไฟล์ใหม่: อ่านไฟล์แต่ละไฟล์ เขียนต่อท้ายในไฟล์ปลายทาง ตัวอย่าง (อ่านทั้งไฟล์ทีละไฟล์) filenames = [file1.txt, file2.txt, file3.txt] with open(output_file, w) as outfile: for fname in filenames: with open(fname) as infile: outfile.write(infile.read()) ตัวอย่าง (
ใช้ os.chmod(file, mode) เปลี่ยน permission mode ต้องเป็น octal (ขึ้นต้นด้วย 0o) เช่น 0o777 >>> import os >>> os.chmod(my_file, 0o777) หรือใช้ flags จากโมดูล stat (รายละเอียด) อีกวิธี ใช้ subprocess เรียก chmod: >>> import subprocess >>> subprocess.call([chmod, 04
ใช้ pwd, grp, os modules เปลี่ยน owner ของไฟล์/ไดเรกทอรี ตัวอย่าง import pwd import grp import os uid = pwd.getpwnam(nobody).pw_uid gid = grp.getgrnam(nogroup).gr_gid path = my_folder os.chown(path, uid, gid)
ใช้ pwd, grp, os modules เปลี่ยน owner ของไฟล์/ไดเรกทอรี ตัวอย่าง import pwd import grp import os uid = pwd.getpwnam(my_name).pw_uid gid = grp.getgrnam(my_group).gr_gid path = my_folder os.chown(path, uid, gid)
ใช้ os.chroot เปลี่ยน root directory ของ current process (Unix only) >>> import os >>> os.chroot(/tmp/my_folder) จะเปลี่ยน root directory ของสคริปต์เป็น /tmp/my_folder
Python ไม่มีวิธี native track ไฟล์ที่เปิดอยู่ทั้งหมด ต้อง track เอง หรือใช้ with statement ที่ปิดอัตโนมัติ ตัวอย่าง with statement with open(file.txt) as f: # do something with f here หรือสร้าง class จัดการไฟล์หลายไฟล์: ตัวอย่าง Class class OpenFiles: def __init__(self): self.files =