What to do with “The input line is too long” error message?

一个人想着一个人 提交于 2019-11-28 21:38:58

You should use the subprocess module instead. See this little doc for how to rewrite os.system calls to use subprocess.

Even it's a good idea to use subprocess.Popen(), this does not solve the issue.

Your problem is not the 255 characters limit, this was true on DOS times, later increased to 2048 for Windows NT/2000, and increased again to 8192 for Windows XP+.

The real solution is to workaround a very old bug in Windows APIs: _popen() and _wpopen().

If you ever use quotes during the command line you have to add the entire command in quoates or you will get the The input line is too long error message.

All Microsoft operating systems starting with Windows XP had a 8192 characters limit which is now enough for any decent command line usage but they forgot to solve this bug.

To overcome their bug just include your entire command in double quotes, and if you want to know more real the MSDN comment on _popen().

Be careful because these works:

prog
"prog"
""prog" param"
""prog" "param""

But these will not work:

""prog param""

If you need a function that does add the quotes when they are needed you can take the one from http://github.com/ssbarnea/tendo/blob/master/tendo/tee.py

You should use subprocess instead of os.system.

subprocess has the advantage of being able to change the directory for you:

import subprocess
my_cwd = r"..\folder\"
my_process = subprocess.Popen(["command name", "option 1", "option 2"], cwd=my_cwd)
my_process.wait() # wait for process to end
if my_process.returncode != 0:
    print "Something went wrong!"

The subprocess module contains some helper functions as well if the above looks a bit verbose.

Assuming you're using windows, from the backslashes, you could write a .bat file from python and then os.system() on that. It's a hack.

Make sure when you're using '\' in your strings that they're being properly escaped.

Python uses the '\' as the escape character, so the string "..\folder\filename" evaluates to "..folderfilename" since an escaped f is still an f.

You probably want to use

r"..\folder\filename"

or

"..\\folder\\filename"

I got the same message but it was strange because the command was not that long (130 characters) and it used to work, it just stopped working one day. I just closed the command window and opened a new one and it worked. I have had the command window opened for a long time (maybe months, it's a remote virtual machine). I guess is some windows bug with a buffer or something.

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