如何获得python在UNIX中的最大文件系统路径长度? [英] How do I get in python the maximum filesystem path length in unix?

查看:529
本文介绍了如何获得python在UNIX中的最大文件系统路径长度?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我维护的代码中,我运行了:

  ctypes.wintypes import MAX_PATH 

我想将其更改为:

  try:
from ctypes.wintypes import MAX_PATH
除了ValueError:#在linux上引发
MAX_PATH = 4096#请参阅注释

但是我找不到任何方法从python( os,os.path,sys)获取最大文件系统路径的值。或者我需要一个外部库吗?



或者在linux下没有类似MAX_PATH的,至少不是发行版中的一个标准?





  try:
MAX_PATH = int(subprocess)
.check_output(['getconf','PATH_MAX','/']))
除了(ValueError,subprocess.CalledProcessError,OSError):
deprint('call getconf failed - error:',traceback = True)
MAX_PATH = 4096


解决方案你可以从文件中读取这个值:

$ $ $ $ $ $ $ $ PATH_MAX(在limits.h中定义)
* FILENAME_MAX(在stdio.h中定义)

或者使用subprocess.check_output()和 getconf 函数:

  $ getconf NAME_MAX / 
$ getconf PATH_MAX /

如下例所示:

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

一个href =http://pubs.opengroup.org/onlinepubs/009695399/functions/fpathconf.html =nofollow> fpath 为文件设置不同的值。


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

解决方案

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.

这篇关于如何获得python在UNIX中的最大文件系统路径长度?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆