how to run my own external command in python script [duplicate] - python

This question already has answers here:
How do I execute a program or call a system command?
(65 answers)
Run a linux system command as a superuser, using a python script
(6 answers)
Closed 9 years ago.
I wanna to run my own non-system external commands in python.
Such as "sudo insteon on 23". Subprocess and os.system are designed for system calls.
Does anybody know how to do it?
Thanks

You can use subprocess.Popen for this:
import shlex
import subprocess
proc = subprocess.Popen(shlex.split('sudo insteon on 23'))
proc.communicate()

Related

How can I redirect os.system() output in Python? [duplicate]

This question already has an answer here:
run a process to /dev/null in python
(1 answer)
Closed 2 years ago.
I am trying to run something inside a Python file, and i do not want to see in the Python console the run output:
os.system("cd ../programs/{0} && ./run".format(project))
How can I do it? I tried with subprocess.call() but it does not compile.
Redirecting stdout to /dev/null:
import subprocess
subprocess.call(['./run'],
cwd=os.path.join('..', 'programs', project),
stdout=subprocess.DEVNULL)

How to run batch file using python script? [duplicate]

This question already has answers here:
Run a .bat file using python code
(9 answers)
Closed 6 years ago.
Is this possible? I yes, can anyone help me how to do it. I don't know how to use subprocess and Popen() to run my sort.bat file.
You can use os.system:
import os
os.system('Path to your .bat file')

Command line interface for python [duplicate]

This question already has answers here:
Python interactive CLI application?
(3 answers)
Closed 6 years ago.
I want to create a program by python, when the program is run, it show its own command line interface. The user can input a command into the interface, and the program will handle that command. Is there any way to do that in both Windows and Linux environment?
this should work for both linux and windows:
from subprocess import call
while True:
print call(raw_input("command: "), shell = True)

how to execute shell command from python [duplicate]

This question already has answers here:
How do I execute a program or call a system command?
(65 answers)
Running a linux command from python
(5 answers)
Closed 8 years ago.
how can I execute the following command in python please
sudo mount --bind /media/networkshare/camera /var/www/media
Technically you could use Python's subprocess module for this (see also this answer):
import subprocess
subprocess.check_call(['sudo', 'mount', '--bind', '/media/networkshare/camera',
'/var/www/media'])
Of course, this will still prompt you for your password. If you don't want it to prompt for a password, then you'll have to setup sudo so that it can execute a single command as root. See the following guide for how to do that:
https://askubuntu.com/questions/155791/how-do-i-sudo-a-command-in-a-script-without-being-asked-for-a-password

Command Line Execution in Python with subprocess? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicates:
Python subprocess
child subprocess kill in python daemon
How can I run commands in the windows command line through python?
I'm trying to have it so that it starts a subprocess, I don't care about the output, but I want to be able to kill it at any time.
subprocess.Popen: http://docs.python.org/library/subprocess.html

Categories

Resources