一种Python脚本提供“用户输入"信息,到另一个python脚本 [英] One Python script giving "user input" to another python script

查看:272
本文介绍了一种Python脚本提供“用户输入"信息,到另一个python脚本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的第一个问题,我希望我做对了. 假设我有这些文件:

this is my first question, I hope I'm doing this right. let's say I have these this file:

"simple.py":

"simple.py":

a=raw_input("your name?")
print "Hello",a

但是使用不同的脚本,我想多次执行"simple.py"并自动输入,就像这样:

but with a different script, I want to execute "simple.py" many time and giving the input automatically, that would work like:

"everyone.py"

"everyone.py"

run simple.py input=Alice
run simple.py input=Bob
...

获得 你好爱丽丝" 你好鲍勃" ...

to get "Hello Alice" "Hello Bob" ...

我知道可以通过使用os.system使"everyone.py"运行"simple.py",但是有什么实际的方法可以做到这一点?如果第一个脚本要求多次输入怎么办?

I know it's possible to make "everyone.py" run "simple.py" by using os.system, but is there any practical way to do something like this? And what if the first script asks for input several times?

重要的是,我无法编辑SIMPLE.PY ,只能编辑其他文件

It's important that I CANNOT EDIT SIMPLE.PY, only the other file

先谢谢您了:)

推荐答案

import sys
print "Hello",sys.argv[1]

运行C:\Python26\python.exe simple.py Alice会产生

你好爱丽丝

Hello Alice

这里有一个很好的示例,说明如何从系统将输入输入到python应用程序中:

There's a good example on how to get input from the system into a python application here:

由于您没有提到您无法修改simple.py,因此您需要自动将内容输入raw_input()中,最快的方法是简单地输入数据进入脚本作为输入":

Since you didn't mention that you can not modify simple.py then you would need to automaticly input things into the raw_input() and the fastest way to do this is simply to pipe in data into the script as "input":

C:>回显爱丽丝" |运行simple.py

C:> echo "Alice" | run simple.py

这篇关于一种Python脚本提供“用户输入"信息,到另一个python脚本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆