Python File truncate() Method
ตัวอย่าง
เปิดไฟล์ที่มี "a" เพื่อต่อท้าย จากนั้นตัดไฟล์ให้เหลือ 20 ไบต์:
f = open("demofile2.txt", "a")
f.truncate(20)
f.close()
#open
and read the file after the truncate:
f = open("demofile2.txt", "r")
print(f.read())
ความหมายและการใช้งาน
วิธี การtruncate()
ปรับขนาดไฟล์เป็นจำนวนไบต์ที่กำหนด
หากไม่ได้ระบุขนาดไว้ ตำแหน่งปัจจุบันจะถูกใช้
ไวยากรณ์
file.truncate(size)
ค่าพารามิเตอร์
Parameter | Description |
---|---|
size | Optional. The size of the file (in bytes) after the truncate. Default None, which means the current file stream position. |