How do I get in python the maximum filesystem path length in unix?

北城余情 提交于 2020-01-04 06:29:28

问题


In the code I maintain I run across:

from ctypes.wintypes import MAX_PATH

I would like to change it to something like:

try:
    from ctypes.wintypes import MAX_PATH
except ValueError: # raises on linux
    MAX_PATH = 4096 # see comments

but I can't find any way to get the value of max filesystem path from python (os, os.path, sys...) - is there a standard way or do I need an external lib ?

Or there is no analogous as MAX_PATH in linux, at least not a standard among distributions ?


Answer

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

回答1:


You can read this values from files:

* PATH_MAX (defined in limits.h)
* FILENAME_MAX (defined in stdio.h)

Or use subprocess.check_output() with getconf function:

$ getconf NAME_MAX /
$ getconf PATH_MAX /

as in the following example:

name_max = subprocess.check_output("getconf NAME_MAX /", shell=True)
path_max = subprocess.check_output("getconf PATH_MAX /", shell=True)

to get values and fpath to set different values for files.



来源:https://stackoverflow.com/questions/32807560/how-do-i-get-in-python-the-maximum-filesystem-path-length-in-unix

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!