Computer >> Hướng Dẫn Máy Tính >  >> Lập Trình >> Python

Lấy giới hạn độ dài tên file tối đa bằng Python (os.statvfs)

Trên UNIX/Linux, giới hạn độ dài tên file (NAME_MAX) có thể khác nhau giữa các filesystem. Dùng os.statvfs() để truy vấn.

Ví dụ

import os
import statvfs  # Python 2; Py3 dùng os.statvfs constants

# Python 3: os.statvfs_result[f_fsid]... constants in os module
print(os.statvfs('/').f_namemax)      # 255 (ext4 typical)
print(os.statvfs('/boot').f_namemax)  # có thể khác (ví dụ 255)

Lưu ý: Module statvfs cũ (Python 2). Python 3 dùng attribute trực tiếp trên kết quả os.statvfs(): .f_namemax.

Cross-platform

Windows: MAX_PATH = 260 (tuy nhiên Windows 10+ hỗ trợ long path nếu enable).

import sys
if sys.platform == 'win32':
    MAX_PATH = 260
else:
    MAX_PATH = os.statvfs('/').f_namemax