繁体   English   中英

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

[英]batch file to enter commands in the prompt

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

 python  
 print "hello"  

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

print "hello"  

如果以上两个命令是在bat文件中编写的,那么python命令实用程序会被打开并在我手动终止后等待,下一条命令将被执行
有什么办法可以将上面的print“ hello”命令重定向到python命令实用程序中

我知道我们可以编写一个单独的带有“ hello”字样的python文件,并直接通过
python filename.py
但是我的要求特别是我上面提到的重定向

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

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

试试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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM