python的 -c 可以在命令行中调用 python 代码, 实际上 -c 就是 command 的意思。
官方文档中解释为(节选自: python docs):
Execute the Python code in command. command can be one or more statements separated by newlines, with significant leading whitespace as in normal module code.
If this option is given, the first element of sys.argv will be "-c" and the current directory will be added to the start of sys.path(allowing modules in that directory to be imported as top level modules).
Raises an auditing event cpython.run_command with argument command.
简单来说, 就是 python -c 可以在命令行中执行 python 代码, 跟把代码放置在 .py 文件中然后运行这个文件比无明显差别。
# python -c "from passlib.hash import sha512_crypt; print sha512_crypt.encrypt('PASSWD')"
# passwd="PASSWD"
# python -c "from passlib.hash import sha512_crypt; print sha512_crypt.encrypt('"${passwd}"')"
参考
python -c 妙用
https://www.cnblogs.com/chnmig/p/14207100.html
加快python -c调用
https://cloud.tencent.com/developer/ask/218048
Python command line and environment
https://docs.python.org/3/using/cmdline.html#cmdoption-c
python执行(-m,-c)如何通过argv查看参数内容
https://jingyan.baidu.com/article/03b2f78c3b38f05ea237ae05.html
网友评论