When replacing a program written in a shell script with Python, it was necessary to call some shell commands from Python, but it seems that shell calls have different character limits for each OS, so I investigated.
os.system()
import os
i = 100000000
n = 0
while True:
n += i
arg = 'b' * n
command = 'echo "{arg}"'.format(arg=arg)
result = os.system(command)
if result != 0:
if i == 1:
break
n -= i
i //= 10
print("LENGTH: ", n -1 + 7) #Add echo, comma, and space
subprocess.run(shell=True)
import subprocess
i = 100000000
n = 0
while True:
try:
n += i
arg = 'b' * n
command = 'echo "{arg}"'.format(arg=arg)
subprocess.run(command,shell=True)
except OSError:
if i == 1:
break
n -= i
i //= 10
print("LENGTH: ", n -1 + 7) #Add echo, comma, and space
OSX sierra (Python3.6.1) Python3 is downloaded from https://www.python.org/
command | os.system | subprocess.run |
---|---|---|
Maximum number of characters | 260554 characters | 260549 characters |
Windows7 (Python3.6.1) Python3 is downloaded from https://www.python.org/
command | os.system | subprocess.run |
---|---|---|
Maximum number of characters | 8160 characters | 32733 characters |
Ubuntu16.04 (Python3.5.2) Python 3 is in the standard repository
command | os.system | subprocess.run |
---|---|---|
Maximum number of characters | 131071 characters | 131071 characters |
CentOS7 (Python3.6.1) Python3 installed from the IUS community repository
command | os.system | subprocess.run |
---|---|---|
Maximum number of characters | 131071 characters | 131071 characters |
I also verified the pattern of shell = False, but I was worried that it could be investigated well, so I did not describe it above. A place where we conducted a survey in which characters are packed in the first argument