Mr_*_*s_D 4 linux filesystems path max-path python-2.7
在我维护的代码中,我遇到了:
from ctypes.wintypes import MAX_PATH
Run Code Online (Sandbox Code Playgroud)
我想将其更改为:
try:
from ctypes.wintypes import MAX_PATH
except ValueError: # raises on linux
MAX_PATH = 4096 # see comments
Run Code Online (Sandbox Code Playgroud)
但我找不到任何方法从 python ( os, os.path, sys...)获取最大文件系统路径的值- 有标准方法还是我需要外部库?
或者在 linux 中没有类似于 MAX_PATH 的东西,至少不是发行版中的标准?
try:
MAX_PATH = int(subprocess.check_output(['getconf', 'PATH_MAX', '/']))
except (ValueError, subprocess.CalledProcessError, OSError):
deprint('calling getconf failed - error:', traceback=True)
MAX_PATH = 4096
Run Code Online (Sandbox Code Playgroud)
正确执行此操作的方法是使用带有前缀名称的os.pathconf或:os.fpathconfPC_
>>> os.pathconf('/', 'PC_PATH_MAX')
4096
>>> os.pathconf('/', 'PC_NAME_MAX')
255
Run Code Online (Sandbox Code Playgroud)
请注意,路径组件的最大长度可能因目录而异,因为它取决于文件系统!
您可以从文件中读取该值:
* PATH_MAX (defined in limits.h)
* FILENAME_MAX (defined in stdio.h)
Run Code Online (Sandbox Code Playgroud)
或者将 subprocess.check_output() 与getconf函数一起使用:
$ getconf NAME_MAX /
$ getconf PATH_MAX /
Run Code Online (Sandbox Code Playgroud)
如以下示例所示:
name_max = subprocess.check_output("getconf NAME_MAX /", shell=True)
path_max = subprocess.check_output("getconf PATH_MAX /", shell=True)
Run Code Online (Sandbox Code Playgroud)
获取值和fpath为文件设置不同的值。
| 归档时间: |
|
| 查看次数: |
3543 次 |
| 最近记录: |