简体   繁体   中英

batch file to enter commands in the prompt

I wanted to write a bat file in which the following are the commands

 python  
 print "hello"  

I wanted it to work like it will open python command utility and execute

print "hello"  

If the above two commands are written in bat file python command utility is getting opened and waiting after I terminate it manually,The next command is getting executed
Is there any way by which I can redirect the above print "hello" command into python command utility

I know we can write a separate python file with print "hello" and call that directly by
python filename.py
But my requirement is specifically the redirection which I have mentioned above

Any help in this regard is greatly appreciated
Thanks in advance!!!

I believe you will need to use the python -c command, followed by the code you wish to execute. The code will need to be properly enclosed in quotes or double quotes (I believe it depends on whether you wish to use multi-line commands).

Try python -c print "hello"

From the docs:

-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

Why don't you create a temp py file dynamically from the BAT ?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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