This question already has answers here:
How do I pass a string into subprocess.Popen (using the stdin argument)?
(12 answers)
Closed 6 years ago.
I am able to open cmd.exe using script
import subprocess
subprocess.call("C:\Windows\System32\cmd.exe",shell=True)
But I am unable to send input command to cmd.exe open.
I want to achieve something like below mentioned using script
1) script give input command like python to cmd.exe open
2) After that script give input command like print "hello" to python prompt comes
Why not use Python to create a batch file that prints what you want, then execute that batch file? You could either return immediately or keep the command interpreter open: it depends on the command switches you use to run the batch file.
You could create a file hello.py containing print "hello", and run it through cmd ?
from subprocess import call
call(["python", "hello.py"])
Related
This question already has answers here:
Execute multiple commands in Paramiko so that commands are affected by their predecessors
(2 answers)
Closed 2 years ago.
I want to run multiple commands with same shell.
Executing command of paramiko library is not with same shell.
Commands :
Ssh cloud-user#node -i /path/to/bcmt_rsa
Cat /etc/ssh/sshd_config
Change permitRootLogin yes to no
Restart sshd server
Log to file.
I am new in python. I have tried many ways but not working
Not clear questing but i have example lets say you want to edit file then read it
have variable
command="echo data >> path_to_file/filename.txt"
command+="cat path_to_file/filename.txt"
then execute ssh normal and read the stdout lines ,or you can use && in the command
like command="echo data >file_path.txt && cat file_path.txt"
Do you want to runultiple comand at a time in single sell?I think its not possible.But you can splot your terminal and can run differend comand in didferent partifion of a terminal.For this you can use Terminator,a great bash terminal.
Besides python is a single threded language.So you can not run multiple function same time in a script.Python will exicute function by function
This question already has answers here:
How to keep a Python script output window open?
(27 answers)
Closed 3 years ago.
In my Python script, I create a .bat file (many actually), and I run them via
os.startfile(blah)
Everything works like expected, however, those terminals die after finishing. I want to keep them open, so that I can type more commands manually in those opened terminals.
How?
You could try using the cmd command to run the batch file, and then use command line arguments for cmd to modify its behavior. For example:
os.startfile("cmd.exe /k blah.bat")
Documentation of the available command line arguments can be found here:
https://learn.microsoft.com/en-us/windows-server/administration/windows-commands/cmd
This question already has answers here:
How do I execute a program or call a system command?
(65 answers)
Closed 3 years ago.
I want to launch a external program like notepad.exe using python. I want to have a script that just runs Notepad.exe.
It's really simple with Python's builtin os module.
This will start Microsoft Notepad:
import os
# can be called without the filepath, because notepad is added to your PATH
os.system('notepad.exe')
Or if you want to launch any other program just use:
import os
# r for raw-string, so don't have to escape backslashes
os.system(r'path\to\program\here\program.exe')
Would recommend the subprocess module. Just build up a list of arguments like you would run in the terminal or command line if you are on windows then run it.
import subprocess
args = ['path\to\program\here\program.exe']
subprocess.call(args)
Check out the docs here for all of the other process management functionality.
this can be done using Python OS. Please see the code below for an example.
import os
os.startfile('test.txt')
Startfile will execute the program associated with the file extension.
This question already has answers here:
Shell Script: Execute a python program from within a shell script
(11 answers)
Closed 5 years ago.
I know how to program a python, but I have no idea about Unix, so how can I create two .sh files to compile and execute my python program. For example, my program named hello.py and I have two files named compile.sh and execute.sh, I want to invoke compile.sh then hello.py will be compiled, and invoke execute.sh then hello.py will be executed. Thanks!
Instead of writing Shell scripts (the .sh files) you can just open a terminal and do stuff from there, see http://www.dummies.com/computers/macs/mac-operating-systems/how-to-use-basic-unix-commands-to-work-in-terminal-on-your-mac/ - whatever you do there is executed live as if it was a Shell script.
By opening a terminal and simply typing
python my_script.py
you trigger python to first compile the code to *.pyc files and then execute it. No special steps are needed! Whatever you type in the terminal, you can also save it as a shell script and run it at a later point. Shell scripting is taught at many websites like https://www.shellscript.sh/ and others, google is your friend.
This question already has answers here:
How do I execute a program or call a system command?
(65 answers)
Closed 8 years ago.
I need to call a program in command line that will be executed in batch mode. This program receives a file with extension .jlink that contains a series of commands that the program JLink.exe will execute and then close.
I tried to execute in the following way:
os.system('C:/SEGGER/JLink_V490d/JLink.exe -CommanderScript D:\Files\CommandFile.jlink')
But this command executes the program in normal mode, not accepting the arguments.
How can I call as execution of command line and then send that command so the program executes in batch mode with those arguments?
Use subprocess:
subprocess.call(["C:\SEGGER\JLink_V490d\JLink.exe",
"-CommanderScript", "D:\Files\CommandFile.jlink"])
You can also use shell=True to just execute a command directly in a shell (what you were trying to do with os.system), but this is not recommended because it's a security hazard.
More info here:
https://docs.python.org/2/library/subprocess.html