how to execute shell command from python [duplicate] - python

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

Related

Get logs of bash code from bash subprocess [duplicate]

This question already has answers here:
Retrieving the output of subprocess.call() [duplicate]
(7 answers)
Running Bash commands in Python
(11 answers)
Closed 3 years ago.
I'm running some bash codes from python using subprocess module, E.G.
import subprocess
container_logs = 'az container logs --name textgeneratorinference'
subprocess.call([container_logs],shell=True)
From there I get a 0 response meaning the code was executed, but when I run the same come in the terminal I get a message E.G:
Login Succeeded
WARNING! Your password will be stored unencrypted in .
Configure a credential helper to remove this warning. See
https://docs.docker.com/engine/reference/commandline/login/#credentials-store
How can I get this into a python variable?

Execute terminal commands in python3 [duplicate]

This question already has answers here:
How do I execute a program or call a system command?
(65 answers)
Closed 7 years ago.
I am on a Raspberry Pi, and I am using a program called fswebcam, which allows you to take pictures with a webcam.
~$ fswebcam image.jpg
That command if entered in terminal takes a picture and saves it to your computer, however I want to build a simple python program that can access the terminal and execute that same command as I have listed above.
I have tried to import os and use os.system('fswebcam image.jpg') But it isn't working for me.
How can I have python execute terminal commands?
Use the subprocess module:
import subprocess
subprocess.Popen(["fswebcam", "image.jpg"])

Python end program in shell [duplicate]

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

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

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

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()

Categories

Resources