Roh*_*han 0 python stdin arguments
Python究竟是如何接收的
echo input | python script
Run Code Online (Sandbox Code Playgroud)
和
python script input
Run Code Online (Sandbox Code Playgroud)
不同?我知道一个是通过stdin来的,另一个是作为一个参数传递的,但是在后端会发生什么不同?
我不确定你在这里有什么困惑.stdin和命令行参数被视为两个不同的东西.
argv与任何其他c程序一样,命令行args在参数中自动传递.编写的Python的主要功能C(即python.c)接收它们:
int
main(int argc, char **argv) // **argv <-- Your command line args
{
wchar_t **argv_copy;
/* We need a second copy, as Python might modify the first one. */
wchar_t **argv_copy2;
/* ..rest of main omitted.. */
Run Code Online (Sandbox Code Playgroud)
管道中的内容存储在stdin您可以通过的内容中sys.stdin.
使用示例test.py脚本:
import sys
print("Argv params:\n ", sys.argv)
if not sys.stdin.isatty():
print("Command Line args: \n", sys.stdin.readlines())
Run Code Online (Sandbox Code Playgroud)
在没有管道的情况下运行它会产生:
(Python3)jim@jim: python test.py "hello world"
Argv params:
['test.py', 'hello world']
Run Code Online (Sandbox Code Playgroud)
使用时echo "Stdin up in here" | python test.py "hello world",我们会得到:
(Python3)jim@jim: echo "Stdin up in here" | python test.py "hello world"
Argv params:
['test.py', 'hello world']
Stdin:
['Stdin up in here\n']
Run Code Online (Sandbox Code Playgroud)
没有严格的相关性,但有趣的是:
另外,我记得你可以stdin使用-Python 的参数执行存储的内容 :
(Python3)jimm@jim: echo "print('<stdin> input')" | python -
<stdin> input
Run Code Online (Sandbox Code Playgroud)
KEWL!
| 归档时间: |
|
| 查看次数: |
585 次 |
| 最近记录: |