批处理文件以在提示中输入命令

时间:2014-11-03 10:35:57

标签: python windows batch-file

我想写一个bat文件,其中以下是命令

 python  
 print "hello"  

我希望它能像打开python命令实用程序并执行

一样工作
print "hello"  

如果以上两个命令都是用bat文件写的 我手动终止后,python命令实用程序正在打开并等待,下一个命令正在执行中 有什么方法可以重定向上面的打印"你好"命令进入python命令实用程序

我知道我们可以用print打印一个单独的python文件"你好"并直接打电话给     python filename.py
但我的要求特别是我上面提到的重定向

非常感谢这方面的任何帮助 提前致谢!!!

3 个答案:

答案 0 :(得分:0)

我相信你需要使用python -c命令,然后是你想要执行的代码。 代码需要用引号或双引号括起来(我相信这取决于你是否希望使用多行命令)。

答案 1 :(得分:0)

尝试python -c print“hello”

来自文档:

-c <command>

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).

https://docs.python.org/2/using/cmdline.html

答案 2 :(得分:0)

为什么不从BAT动态创建临时py文件?

@echo off
set $Command=print "hello"
echo %$command% >temp.py
python temp.py
del temp.py
echo next
pause & exit /b