ใช้ os.remove() ลบไฟล์เดี่ยว: >>> import os >>> os.remove(my_file.txt) อาร์กิวเมนต์ต้องเป็น absolute หรือ relative path
ใช้ os.remove() ลบไฟล์เดี่ยว: >>> import os >>> os.remove(my_file.txt) อาร์กิวเมนต์ต้องเป็น absolute หรือ relative path
ลบไฟล์เดี่ยว ใช้ os.remove: ตัวอย่าง >>> import os >>> os.remove(my_file.txt) ลบหลายไฟล์ วนลูป os.remove ลบโฟลเดอร์ทั้งหมด ใช้ shutil.rmtree: >>> import shutil >>> shutil.rmtree(my_folder) ลบตาม Pattern ด้วย Regex import os, re, os.path pattern = ^your_regex_here$
ตรวจสอบก่อนด้วย os.path.exists() แล้วค่อยสร้าง: import os if not os.path.exists(my_folder): os.makedirs(my_folder) หรือใช้ EAFP pattern: import os, errno try: os.makedirs(my_folder) except OSError as e: if e.errno != errno.EEXIST: raise
ตรวจสอบก่อนด้วย os.path.exists() แล้วค่อยสร้าง: import os if not os.path.exists(my_folder): os.makedirs(my_folder) หรือใช้オイディอม EAFP (ลองสร้าง catch error): import os, errno try: os.makedirs(my_folder) except OSError as e: if e.errno != errno.EEXIST: raise
ใช้ os.makedirs(path, exist_ok=True) สร้างโฟลเดอร์ recursive อนุญาตให้ parent มีอยู่แล้ว import os os.makedirs(my_folder/another/folder, exist_ok=True)
mkdir -p สร้างโฟลเดอร์ recursive แม้ parent จะมีอยู่แล้ว ใช้ os.makedirs(path, exist_ok=True) (Python 3.2+) >>> import os >>> os.makedirs(my_folder/another/folder, exist_ok=True) หมายเหตุ: พารามิเตอร์คือ exist_ok ไม่ใช่ exists_ok
เปลี่ยน directory (cd) ด้วย os.chdir() รองรับ relative/absolute path ตัวอย่าง >>> import os >>> os.chdir(my_folder)
เปลี่ยน directory (cd) ด้วย os.chdir() รองรับ relative/absolute path ตัวอย่าง >>> import os >>> os.chdir(my_folder)
เปลี่ยน directory (cd) ด้วย os.chdir() รองรับ relative/absolute path ตัวอย่าง >>> import os >>> os.chdir(my_folder)
ใช้ os.getcwd() หา current working directory (pwd) ตัวอย่าง >>> import os >>> print(os.getcwd()) /home/ayush/qna
ใช้ os.path.realpath(__file__) หาพาธเต็มของไฟล์ปัจจุบัน ตัวอย่าง >>> import os >>> print(os.path.realpath(__file__)) /home/ayush/qna/path.py
ลบโฟลเดอร์ว่าง ใช้ os.rmdir: ตัวอย่าง >>> import os >>> os.rmdir(my_folder) ลบโฟลเดอร์ที่มีเนื้อหา ใช้ shutil.rmtree: >>> import shutil >>> shutil.rmtree(my_folder)
ลบโฟลเดอร์ที่มีไฟล์/ซับโฟลเดอร์ครบ ใช้ shutil.rmtree: >>> import shutil >>> shutil.rmtree(my_folder)
ปิดไฟล์ด้วย close() บน file object: ตัวอย่าง >>> f = open(hello.txt, r) >>> # Do stuff with file >>> f.close() ไม่แนะนำ เปิดแบบนี้ เพราะไม่ปลอดภัย (อาจไม่ปิดถ้า error) ใช้ with แทน: ตัวอย่าง with open(hello.txt, r) as f: print(f.read()) ไฟล์จะปิดอัตโนมัติเมื่อออกจาก wi
Python buffer การเขียนไฟล์ (file.write คืนค่าก่อนเขียนจริง) เพื่อความเร็ว ใช้ file.flush() บังคับ flush ทันที with open(my_file.txt, w+) as file: file.write(foo) file.write(bar) file.flush()
File Descriptors - low-level facility จาก OS kernel เป็น integer ระบุไฟล์เปิดใน kernel table ต่อ process ใช้กับ system calls แต่ยุ่งยาก (fixed buffers, retries, manual error handling) File Objects - Python classes wrap file descriptors ให้สะดวก ปลอดภัย มี error handling, buffering, line-by-line read
ใช้ isatty() ตรวจสอบว่า script รันใน terminal หรือถูก pipe import sys if sys.stdout.isatty(): print(Inside a terminal!) else: print(Piped output) รันจาก terminal จะได้: Inside a terminal!
File Descriptors - low-level facility จาก OS kernel เป็น integer ระบุไฟล์ในตาราง open files ของแต่ละ process ใช้กับ system calls แต่ยุ่งยาก (fixed buffers, retries, manual error handling) File Objects - Python classes wrap file descriptors ทำให้สะดวก ปลอดภัย มี error handling, buffering, line-by-lin
ใช้ 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)
f.read(size) อ่านข้อมูลจำนวน size bytes คืนค่า string size เป็น optional ถ้าไม่ระบุหรือ negative จะอ่านทั้งไฟล์ ถ้าจบไฟล์คืนค่า empty string ตัวอย่าง: อ่าน 10 ตัวอักษร ASCII >>> f = open(my_file, r) >>> print(f.read(10)) Hello worl >>> f.close()