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

การหาขีดจำกัดความยาวชื่อไฟล์สูงสุดใน Python (os.statvfs)

บาง Platform ขีดจำกัดความยาวชื่อไฟล์ (Filename Length Limit) แตกต่างกันไปตาม Filesystem ที่ Mount อยู่

ตัวอย่าง: ตรวจสอบ Limit บน Root และ /boot

>> import os, statvfs
>>> os.statvfs('/')[statvfs.F_NAMEMAX]
4032

>>> os.statvfs('/boot')[statvfs.F_NAMEMAX]
255

คำอธิบาย

  • os.statvfs(path) คืน os.statvfs_result (named tuple) ข้อมูล Filesystem
  • statvfs.F_NAMEMAX = Index ของ Maximum Filename Length
  • Root (/) ใช้ Filesystem ที่รองรับชื่อยาว (4032 ตัวอักษร)
  • /boot ใช้ Filesystem แบบเก่า (ext2/3?) จำกัด 255 ตัวอักษร

ทางเลือก: ใช้ os.pathconf (Portable)

>> import os
>>> os.pathconf('/', 'PC_NAME_MAX')
255
>>> os.pathconf('/home', 'PC_NAME_MAX')
255

os.pathconf เป็น POSIX Standard ทำงานได้บน Unix-like ทุกตัว

Windows

Windows (NTFS) จำกัดที่ 255 ตัวอักษร (MAX_PATH = 260 รวม Path) สามารถเปิดใช้ Long Paths ได้ผ่าน Group Policy/Registry (Windows 10 1607+)