Python end program in shell [duplicate] - python

This question already has answers here:
Interacting with program after execution
(3 answers)
Closed 7 years ago.
I want a script to run an then finish on the python shell with all variables and methods:
$ python myprogram.py
...
program output
...
>>>
And with #!/usr/bin/python is posible? so I double-click and it just works?

Sounds like you want Python's i flag. From the help menu:
-i : inspect interactively after running script; forces a prompt even
if stdin does not appear to be a terminal; also PYTHONINSPECT=x
So the full command would be
python -i yourscriptname.py

Related

Running a python script with inputs from another python script [duplicate]

This question already has answers here:
How do I write to a Python subprocess' stdin?
(5 answers)
Closed 2 years ago.
I have lots of python scripts to be run and I want to automate them. All of them accept inputs at certain times (3 in this case). I have tried something like this but since echo does not have an EOF it did not work:
os.system("echo 4 | echo 5 | echo 6 | python script.py")
I cannot change the content of script.py and it does not accept arguments to it.
How can I automatically input using a line(s) of python code? Thanks.
Check out Pexpect:
Pexpect is a pure Python module for spawning child applications;
controlling them; and responding to expected patterns in their output.
Pexpect works like Don Libes’ Expect. Pexpect allows your script to
spawn a child application and control it as if a human were typing
commands.

Is there a way to run several consecutive lines in the system command window using python [duplicate]

This question already has answers here:
Execute Commands Sequentially in Python?
(5 answers)
Closed 3 years ago.
I have the following line of commands used in sequence from the command window.
cd path 1 && file.bat && cd path 2 && printfNav path3 > Testing.txt
Is it possible to have this done through python?
You are looking for the os.system() method. You will need to import os into your python program and then using this method you can send commands to the terminal. The method takes in a string and acts as if you typed that into your terminal. For example os.system('pwd') would cause the terminal to print out your current working directory, and so on so forth with other commands.

How do I input data into a command prompt from within python 2.7 [duplicate]

This question already has answers here:
Running windows shell commands with python
(7 answers)
Closed 4 years ago.
I want to open a cmd and then input data into the command line from python. I plan to call PEST calibration software to open from within python and I want to start by opening a cmd.
I am using Python 2.7 and so subprocess doesn't seem to work. I have tried os.system('cmd') and I can open the prompt but I can't input any data.
import os
os.system('cmd')
You should be able to pass the exact resulting string to os.system(). Ex:
os.system('notepad.exe')
In other words, os.system behaves the same way a console would.

Running shell commands within python interpreter [duplicate]

This question already has answers here:
How do I execute a program or call a system command?
(65 answers)
Closed 8 years ago.
Is there a simple method for calling shell command line arguments (like ls or pwd) from within python interpreter?
In plain python, you need to use something along the lines of this:
from subprocess import check_output
check_output("ls", shell=True)
In IPython, you can run either of those commands or a general shell command by starting off with !. For example
! echo "Hello, world!" > /tmp/Hello.txt
If you're using python interactively, you would almost certainly be happier with IPython.
If you meant to use the Python shell interactively while being able to call commands (ls, pwd, ...) check out iPython.

Using arguments in python [duplicate]

This question already has answers here:
How to read/process command line arguments?
(22 answers)
Closed 8 years ago.
I wrote a code using Python in Geany within windows and I'm using arguments in the code. I execute the program within the Geany so I don't know how to use arguments.
How could I convert the program to be be run as a standalone system, not to be run from within an IDE.
how could I use terminal in windows to run the code like this :
John~/home/args -> ./test.py -h
In Notepad, write what you would in terminal...
python test.py -h
Save it as a .bat file... Then you can run the bat file :)
PS. This goes for anything you wish to run in CMD

Categories

Resources