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

Cách lấy thông tin cấu hình hệ thống liên quan file mở bằng Python?


Gọi hàm fpathconf(file_descriptor, name) lấy thông tin cấu hình hệ thống liên quan file mở. name chỉ định giá trị cấu hình cần lấy; có thể là string tên giá trị hệ thống định nghĩa; các tên quy định trong nhiều tiêu chuẩn. Chỉ dùng trên Unix. Ví dụ:

import os, sys
# Mở file
fd = os.open("foo.txt", os.O_RDWR | os.O_CREAT)
# Lấy max số link tới file
no = os.fpathconf(fd, 'PC_LINK_MAX')
print("Maximum number of links to the file. :%d" % no)
# Lấy max độ dài filename
no = os.fpathconf(fd, 'PC_NAME_MAX')
print("Maximum length of a filename :%d" % no)
os.close(fd)

Chạy cho kết quả:

Maximum number of links to the file. :127
Maximum length of a filename :255