How do I get the ORIGINAL command line in Python? with spaces, tabs, etc [duplicate]

雨燕双飞 提交于 2020-01-24 04:54:53

问题


Possible Duplicate:
Full command line as it was typed

sys.argv is already a parsed array, losing double quotes, double spaces and maybe even tab characters (it all depends on the OS/shell, of course).

How can I access the original string before parsing?


回答1:


Shortly, you don't.

Long: on Unix command line is parsed by the calling program and by the time python starts you already have the command line parsed.

PS. On Windows it is possible, but I suppose you are looking for a general response.




回答2:


You can't do that explicitly because, this is how a shell passes the arguments to a program.




回答3:


The sys.argv is all Python got. The shell processed the filename generation (globs), parameter (variable) expansion, quotes, and word splitting before passing the arguments to the Python process (in Unix; in Windows it's the startup actually parsing it, but for portability, you can't rely on that).

However, remember that POSIX shell quoting rules allow passing any characters you may want (except NUL bytes that terminate strings).

Compare starting a process from Python using subprocess.call with or without the shell argument set. With shell=False the list of strings is what comes up in the sys.argv in the started process (starting with the script path; parameters processed by Python itself are removed) while with shell=True the string is passed to the shell which interprets it according to its own rules.



来源:https://stackoverflow.com/questions/6184925/how-do-i-get-the-original-command-line-in-python-with-spaces-tabs-etc

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