从 cmd shell 管道到 python 脚本 [英] Piping to a python script from cmd shell
问题描述
我有一个 python 脚本(不是我创建的),我们称之为 myscript,我用几个参数调用它.所以我在 Windows cmd 中运行这样的脚本:代码:
/wherever/myscript --username=whoever/some/other/path/parameter
然后出现一个提示,我可以将参数传递给 python 脚本:
进程启动成功,blablaPython 2.7.2 blabla(日志控制台)>>>
然后我写我的东西,然后退出回到 cmd:
<预><代码>>>>命令 1()>>>命令 2()>>>放弃()我怀疑这部分出现了一些错误,但在一百次试验中只有一次.所以我想通过脚本来完成.我想将内部 command1 command2 通过管道传输到此脚本,以便我可以测试此函数一千次并查看它何时中断.我有以下一段代码:
echo 'command1()' |py -i/wherever/myscript --username=whoever/some/other/path/parameter
不幸的是,这不会产生与手动输入相同的行为.我可以用管道/重定向输出模拟这种行为吗?为什么不起作用?我希望脚本等待命令时会输入command1()"文本,但似乎我错了.
谢谢!
编辑 16/02/2021 下午 3:33:
- 我正在寻找 cmd shell 方法来解决这个问题,没有 python 的东西
- 剧本
echo 'command1()' |py -i/wherever/myscript --username=whoever/some/other/path/parameter
几乎是正确的,只需删除 '' :
echo command1() |py -i/wherever/myscript --username=whoever/some/other/path/parameter
我的问题来自 myscript.一旦我修复了这一边的奇怪事情,这部分就一切正常了.您甚至可以将所有命令放在一起:
echo command1();command2();quit();|py -i/wherever/myscript --username=whoever/some/other/path/parameter
此问题改编自 unix.com 上 23/08/2012 的 gplayersv 问题,但最初的目的使问题没有得到解答.
Python 的内置 fileinput
模块使这变得简单明了:
#!/usr/bin/env python3导入文件输入使用 fileinput.input() 作为 f:对于 f 中的行:打印(行,结束='')
你可以接受任何对你来说更容易的机制的输入:
$ ls |./filein.py$ ./filein.py/etc/passwd$ ./filein.py <$(uname -r)
I have a python script (not created by me), let's call it myscript, which I call with several parameters. So I run the script like this in Windows cmd: Code:
/wherever/myscript --username=whoever /some/other/path/parameter
And then a prompt appears and I can pass arguments to the python script:
Process started successfully, blabla
Python 2.7.2 blabla
(LoggingConsole)
>>>
And I write my stuff, then quit to be back into cmd:
>>> command1()
>>> command2()
>>> quit()
I suspect some errors occurring in this part, but only once for a hundred trials. So I want to do it by a script. I want to pipe to this script the internal command1 command2, so that I can test this function thousand times and see when it breaks. I have the following piece of code:
echo 'command1()' | py -i /wherever/myscript --username=whoever /some/other/path/parameter
This unfortunately doesn't generate the same behaviour, as if it would be manually entered. Can I simulate this behaviour with pipes/redirecting output? Why doesn't it work? I expect that the 'command1()' text will be entered when the script waits for the commands, but it seems I'm wrong.
Thanks!
EDIT 16/02/2021 3:33PM :
- I was looking for the cmd shell way to solve this, no python stuff
- The piece of script
echo 'command1()' | py -i /wherever/myscript --username=whoever /some/other/path/parameter
is almost correct, just remove the '' :
echo command1() | py -i /wherever/myscript --username=whoever /some/other/path/parameter
my issues were coming from myscript. Once I fixed the weird things on this side, this part was all ok. You can even put all commands together:
echo command1();command2();quit(); | py -i /wherever/myscript --username=whoever /some/other/path/parameter
This question is adapted from a question of gplayersv the 23/08/2012 on unix.com, but the original purpose made the question not answered.
Python's built-in fileinput
module makes this simple and concise:
#!/usr/bin/env python3
import fileinput
with fileinput.input() as f:
for line in f:
print(line, end='')
Than you can accept input in whatever mechanism is easier for you:
$ ls | ./filein.py
$ ./filein.py /etc/passwd
$ ./filein.py < $(uname -r)
这篇关于从 cmd shell 管道到 python 脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!