开发者

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

开发者 https://www.devze.com 2023-03-09 04:25 出处:网络
This question already has answers here: Closed 11 years ago. 开发者_运维百科 Possible Duplicate:
This question already has answers here: Closed 11 years ago. 开发者_运维百科

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?


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.


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消