admin管理员组

文章数量:1123935

In Python, is there a way to get the maximum number of characters that can be passed to a subprocess on the host in question?

I'm asking for implementing xargs-like functionality, i.e. running a command on a (potentially huge) list of file paths collected previously by python functions, so I'd need to know how many args/chars I can pass at a time without exceeding limits, and divide into multiple calls if necessary.

In Python, is there a way to get the maximum number of characters that can be passed to a subprocess on the host in question?

I'm asking for implementing xargs-like functionality, i.e. running a command on a (potentially huge) list of file paths collected previously by python functions, so I'd need to know how many args/chars I can pass at a time without exceeding limits, and divide into multiple calls if necessary.

Share Improve this question asked yesterday mara004mara004 2,31213 silver badges30 bronze badges
Add a comment  | 

1 Answer 1

Reset to default 1

On Unix you can use

max_args = os.sysconf('SC_ARG_MAX')

to get the byte limit on the combined argument list and environment. This may include the null terminators at the end of each argument and environment variable.

So when determining how much space you have for arguments, you should subtract

env_size = sum(len(var)+1 for var in os.environ)

There's no equivalent on Windows, but typically the limit is 32,768 bytes.

本文标签: pythonGet char limit for subprocess argsStack Overflow