簡體   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