python 文件路径最大长度,如何获得python在UNIX中的最大文件系统路径长度?

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 ?

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

解决方案

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值