当前位置: 动力学知识库 > 问答 > 编程问答 >

python - Optparse- Preserving special characters

问题描述:

I'm using optparse and recently I'm encountering the following problem- I want to pass as an argument a directory name. Something like:

./script.py --dir c:\a\b

However, optparse eliminates the "\" sign so the related variable remains with the value c:ab

Note: This script is being run on Linux machine, that connects to Windows machine and does some stuff. This is why I use the ./ in the script name and the directory is Windows sytle.

How do I make optparse preserve the special characters?

网友答案:

This is not a Python problem; it is your shell that parses the backslashes in the command line before sending it to Python. optparse is not to blame.

Escape them by doubling the slashes:

./script.py --dir c:\\a\\b

or put quotes around the argument:

./script.py --dir "c:\a\b"
分享给朋友:
您可能感兴趣的文章:
随机阅读: