How do I execute a program or call a system command?
Use the subprocess
module in the standard library:
import subprocess
subprocess.run(["ls", "-l"])
The advantage of subprocess.run
over os.system
is that it is more flexible (you can get the stdout
, stderr
, the "real" status code, better error handling, etc...).
Even the documentation for os.system
recommends using subprocess
instead:
The
subprocess
module provides more powerful facilities for spawning new processes and retrieving their results; using that module is preferable to using this function. See the Replacing Older Functions with the subprocess Module section in thesubprocess
documentation for some helpful recipes.
On Python 3.4 and earlier, use subprocess.call
instead of .run
:
subprocess.call(["ls", "-l"])
How do I execute a program or call a system command?
Use the subprocess
module in the standard library:
import subprocess
subprocess.run(["ls", "-l"])
The advantage of subprocess.run
over os.system
is that it is more flexible (you can get the stdout
, stderr
, the "real" status code, better error handling, etc...).
Even the documentation for os.system
recommends using subprocess
instead:
The
subprocess
module provides more powerful facilities for spawning new processes and retrieving their results; using that module is preferable to using this function. See the Replacing Older Functions with the subprocess Module section in thesubprocess
documentation for some helpful recipes.
On Python 3.4 and earlier, use subprocess.call
instead of .run
:
subprocess.call(["ls", "-l"])
How do I execute a Shell built-in command with a C function?
You should execute sh -c echo $PWD
; generally sh -c
will execute shell commands.
(In fact, system(foo)
is defined as execl("sh", "sh", "-c", foo, NULL)
and thus works for shell built-ins.)
If you just want the value of PWD
, use getenv
, though.
How to execute a shell program taking inputs with python?
I'll try and give you some hints to get you started - though bear in mind I do not know any of your tools, i.e. waf
or csp-client
, but hopefully that will not matter.
I'll number my points so you can refer to the steps easily.
Point 1
If waf
is a build system, I wouldn't keep running that every time you want to run your csp-client
. Just use waf
to rebuild when you have changed your code - that should save time.
Point 2
When you change directory to /home/augustin/workspaceGS/gs-sw-nanosoft-product-interface-application-2.5.1
and then run ./build/csp-client
you are effectively running:
/home/augustin/workspaceGS/gs-sw-nanosoft-product-interface-application-2.5.1/build/csp-client -k/dev/ttyUSB1
But that is rather annoying, so I would make a symbolic link to that that from /usr/local/bin
so that you can run it just with:
csp-client -k/dev/ttyUSB1
So, I would make that symlink with:
ln -s /home/augustin/workspaceGS/gs-sw-nanosoft-product-interface-application-2.5.1/build/csp-client /usr/local/bin/csp-client
You MAY need to put sudo
at the start of that command. Once you have that, you should be able to just run:
csp-client -k/dev/ttyUSB1
Point 3
Your Python code doesn't work because every os.system()
starts a completely new shell, unrelated to the previous line or shell. And the shell that it starts then exits before your next os.system()
command.
As a result, the cmp ident
command never goes to the csp-client
. You really need to send the cmp ident
command on the stdin
or "standard input" of csp-client
. You can do that in Python, it is described here, but it's not all that easy for a beginner.
Instead of that, if you just have aa few limited commands you need to send, such as "take a picture", I would make and test complete bash
scripts in the Terminal, till I got them right and then just call those from Python. So, I would make a bash
script in your HOME directory called, say csp-snap
and put something like this in it:
#/bin/bash
# Extend PATH so we can find "/usr/local/bin/csp-client"
PATH=$PATH:/usr/local/bin
{
# Tell client to take picture
echo "nanoncam snap"
# Exit csp-client
echo exit
} | csp-client -k/dev/ttyUSB1
Now make that executable (only necessary once) with:
chmod +x $HOME/csp-snap
And then you can test it with:
$HOME/csp-snap
If that works, you can copy the script to /usr/local/bin
with:
cp $HOME/csp-snap /usr/local/bin
You may need sudo
at the start again.
Then you should be able to take photos from anywhere just with:
csp-snap
Then your Python code becomes easy:
os.system('/usr/local/bin/csp-snap')
How to execute system command with an argument that contains spaces
Although depending on the operating system, my first guess is: Surround the argument by quotes like this:
int status = system("ArgumentProgram.exe User1 \"File 1\" Open");
Execute a command by reading contents of a text file
This should work:
import subprocess
# read in users and strip the newlines
with open('/tmp/users.txt') as f:
userlist = [line.rstrip() for line in f]
# get list of commands for each user
cmds = []
for user in userlist:
cmds.append('smbmap -u {} -p p@ssw0rd -H 192.168.2.10'.format(user))
# results from the commands
results=[]
# execute the commands
for cmd in cmds:
results.append(subprocess.call(cmd, shell=True))
# check for which worked
for i,result in enumerate(results):
if result == 0:
print(cmds[i])
Edit: made it your file path, changed to .format(), checked result == 0 (works for ssh trying passwords)
Edit: forgot to add shell=True
How to properly use system() to execute a command in C++?
Give a try with (that is, surrounding cuobjdump.exe path with "
, properly escaped in C++ as \"
):
system("\"C:\\program files\\nvidia gpu computing...\\cuobjdump.exe\" --dump-cubin C:\\..\\input.exe");
Related Topics
How to Create a Density Plot in Matplotlib
Flatten an Irregular List of Lists
Importing Files from Different Folder
How Is Returning the Output of a Function Different from Printing It
Return Json Response from Flask View
How to Do a Case-Insensitive String Comparison
How to Import Other Python Files
How to Terminate Process from Python Using Pid
In Python Script, How to Set Pythonpath
Webdriverwait Not Working as Expected
What Do Lambda Function Closures Capture
How to Sort a Dictionary by Key