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 pyfile 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
Related
I'm using this tutorial: https://silentcrash.com/2016/12/run-python-script-notepad/
The example for Python 3 includes the folder "Program Files (x86)", which I assume is not necessary, but I would still like to know how to fix this problem if it comes up in the future and I don't have another way of doing it.
So what happens is that the program assumes the space is the end of the command and it move on to the argument as seen here:
The Program to Run
Error
The system cannot find the file specified.
An attempt was made to execute the below command.
------------------------------------------------
Command: C:\Program
Arguments: Files\Python37\Lib\idlelib.py "new 2"
Error Code: 2
Is there any way to write the file path such that it won't be split up like this?
You need to surround your arguments with quotes when they contain spaces. In your example, you would use the following command in the Run... dialog:
"C:\Program Files\Python37\Lib\idlelib.py" "$(FULL_CURRENT_PATH)"
I found a helpful hint on superuser.com that will stop the console window from closing when your script finishes. However, note that it now needs an extra set of quotes.
cmd /k ""C:\Program Files\Python37\Lib\idlelib.py" "$(FULL_CURRENT_PATH)""
Dear fellow developers,
I'm repeatedly using (and developing) a python script for calculations, by executing it through the windows command prompt in each test.
The script has some parsed options.
In order to make each of my calculations easily reproducible, I save the actual command I entered to execute each calculation. For the moment I simply copy by hand the command once I executed it and I put it in a file. But since I have to do it for each calculation, I wonder is there is any python script line that could take my command line input, like:
python script.py --option="foo"
into a file.
The form of the command could be:
%save file=_command_used.txt% python script.py --option=foo
which would create the file and save the actual command "python script.py --option=foo" into it.
Thanks in advance!
Best regards!
I would love to have solutions for both Windows command prompt and Linux shell command prompt.
On Linux there is the script command that will capture all entered commands in a file. Use it like that:
script -a _command_used.txt
python script.py --option=foo
python script.py --option=bar
The -a option stands for append so the _command_used.txt will not be overwritten.
On Windows you can achieve a similar thing using Start-Transcript and Stop-Transcript cmdlet. See this related post.
Since you are using Python, I recommend you investigate the Xonsh shell as one way to solve this. It is cross platform and is scripted with python.
How to pass the output of one .py script which I executed in GDB using below command:
(gdb)source myfilename.py
to another .py script myfilename2.py
E.g: On running myfile myfilename.py in GDB I get the below results
(gdb)source myfilename.py
(gdb)print $function("input")
(gdb)$1 = someoutput`
If I want to pass $1 as input to another .py script how do I do it.
Thanks for the help.
I'm not sure if you can run other python programs output to another in gdb, but of course you can pass output of a command like this.
(gdb) run "`python -c 'print "\xff\xff\xff\xff"'`"
May be you can try passing the second .py file instead of the command for its output to be passed into first .py.
You cannot use contents of GDB variable $1 for your program input, since program launch is performed by shell, not by gdb.
You can store your script output in file, launch gdb for your executable and use command
(gdb)run <outputfile
Or use a named pipe, as suggested in this answer.
I try use the super admin and use #!/usr/bin/python at the top however it also doesn't work
Actually, I want to open the .py document but terminal shows that print: command not found.
Just type python in your terminal to invoke the interactive shell. You gave the command in the bash terminal beginning with #, which inactivated the whole line.
If you want to run a python script, then put your code in a file like this:
script.py
#!/usr/bin/python
print "2"
Now you can run the code typing python script.py.
The first line of the code is the shebang line, which specifies which interpreter to use, so you can run your script typing ./script.py.
I am starting fresh with python and trying to execute a code from the python command window. I wrote a file on Desktop\practice\new.py and lunched the python command window.
when I type
C:\users\user\Desktop\practice\new.py
it gives me
SyntaxError: invalid syntax
Executing from CMD worked, but from python window didnt!
Any help?
EDIT2: when i put the compiled code in the directory and use the 'import' it runs, but when the compiled is not in the same directory it won't execute
EDIT: the file contains a simple print statement nd is sytax error free
Everything is explained in here: http://docs.python.org/faq/windows.html#how-do-i-run-a-python-program-under-windows
The main point that when you launch python shell. Its like a live programming. Try to type in it:
>>> print 'hello world'
If you want to launch your file - run in cmd: python C:/users/user/Desktop/practice/new.py
UPDATE: If you do want to run file from within python shell - it was answered here: How to execute a file within the python interpreter?
When you say you're using the "python command window" I'm guessing you mean IDLE...? If so, rather than try to type a command to run a script you've already created as a file, just use File > Open to open that file and then press F5 to run it. Good luck!
The python command window is expecting python commands. Try typing 'import system' or 'print 1+2'.
If you want to run the code in another file you need to use 'import'. Its easier if you start in the same directory, in which case just doing 'import new' will work.
However, there's already a 'new' module in the python library, so the easiest thing to do is to rename your file something else...
It is not working because you are entering the path like c:\users\user\desktop\practice\new.py.....
now try this way: c:/users/user/desktop/practice/new.py
I hope this will work for you i.e. just change '\' to '/'
have a try...
You can run the file like this:
execfile(r'C:\users\user\Desktop\practice\new.py')
Edit: read the comments below this answer before trying it!
Try this:
import sys
sys.path.append("C:\users\user\Desktop\practice\")
import new #won't work - call it something other than new.py...