This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Python subprocess wildcard usage
Using the Python 2.6 subprocess module, I need to run a command on a src.rpm file that I am building with a previous subprocess call.
Unfortunately, I am working with spec files that are not consistent, so I only have a vague idea of what the filename of the src.rpm should look like (for instance, I know the name of the package and the extension in something named "{package}-{version}.src.rpm" but not the version).
I do know, however, that I will only have one src.rpm file in the directory that I am looking, so I can call mock with a command like
mock {options} *.src.rpm
and have it work in shell, but subprocess doesn't seem to want to accept the expansion. I've tried using (shell=True) as an argument to subprocess.call() but even if it worked I would rather avoid it.
How do I get something like
subprocess.call("mock *.src.rpm".split())
to run?
Use the glob package:
import subprocess
from glob import glob
subprocess.call(["mock"] + glob("*.src.rpm"))
The wildcard * has to be interpreted by the SHELL. When you run subprocess.call, by default it doesn't load a shell, but you can give it shell=True as an argument:
subprocess.call("mock *.src.rpm".split(), shell=True)
Related
This question already has answers here:
Calling the "source" command from subprocess.Popen
(9 answers)
Closed 5 years ago.
Hello I am trying to source of file say check.setup from my pythons script.
Code
import os
os.system("source /fullpathhere/check.setup")
It says command not found. Surprisingly I can source the same file directly from the shell.
check.setup is csh file. It is sourcing other .csh files and setting few environment variable I saw few answers here but no one could possibly solve the problem.
PS: I tried to write bash file instead of python. I also tried using subprocess.Popen. Problem persists.
Aashish
use popen
subprocess.Popen('source ./fullpathhere/check.setup', shell=True)
Why didn't tag your posting as csh, even though you explicitly mention csh in your question....
There is no executable namend source (you can verify it by typing bin/which source. Well, you pointed out by yourself, that this is supposed to be done by csh, but how should Python know that it needs to invoke csh, if you don't tell it?
I don't have a csh on my system, but if I remember right from the time a couple of decades ago, where I indeed used csh for programming, you can do something like
os.system("csh -c fullpathhere/check.setup")
Actually, I would also specify the -f flag for skipping .cshrc, unless you really need that to be sourced as well.
This question already has answers here:
Running Bash commands in Python
(11 answers)
Closed 2 years ago.
I am trying to run both Python and bash commands in a bash script.
In the bash script, I want to execute some bash commands enclosed by a Python loop:
#!/bin/bash
python << END
for i in range(1000):
#execute‬ some bash command such as echoing i
END
How can I do this?
Use subprocess, e.g.:
import subprocess
# ...
subprocess.call(["echo", i])
There is another function like subprocess.call: subprocess.check_call. It is exactly like call, just that it throws an exception if the command executed returned with a non-zero exit code. This is often feasible behaviour in scripts and utilities.
subprocess.check_output behaves the same as check_call, but returns the standard output of the program.
If you do not need shell features (such as variable expansion, wildcards, ...), never use shell=True (shell=False is the default). If you use shell=True then shell escaping is your job with these functions and they're a security hole if passed unvalidated user input.
The same is true of os.system() -- it is a frequent source of security issues. Don't use it.
Look in to the subprocess module. There is the Popen method and some wrapper functions like call.
If you need to check the output (retrieve the result string):
output = subprocess.check_output(args ....)
If you want to wait for execution to end before proceeding:
exitcode = subprocess.call(args ....)
If you need more functionality like setting environment variables, use the underlying Popen constructor:
subprocess.Popen(args ...)
Remember subprocess is the higher level module. It should replace legacy functions from OS module.
I used this when running from my IDE (PyCharm).
import subprocess
subprocess.check_call('mybashcommand', shell=True)
This question already has answers here:
How do I execute a program or call a system command?
(65 answers)
Closed 9 years ago.
I want to perform various Linux commands/operations using python script. I will be using the output, verifying/processing it and continue with some more commands execution in my script, may be remote execution also sometimes.
I have tried with both os and subprocess modules. The caveat here is, I am not able to combine both of them i.e. system calls or commands executed from one module does not affect "program/python" environment variables rather only considered by that particular module.
For. ex.
os.chdir(dirname)
os.system(cmd)
# p = subprocess.Popen(cmd)
Now, here changes from os.chdir are not useful for subprocess call. We have to stick with any one of them. If I use subprocess, I have to pass/create shell commands for it.
Added: cwd= is a solution for subprocess.Popen but every time I would have to pass option cwd as argument to future commands, if they all should be run from that dir
Is there a better way where we can use both of these modules together?
Or
Is there any other better module available for command executions.
Also I would like to know "Pros-Cons/Caveats" of both these modules.
os.system always runs /bin/sh, which parses the command string. This can be a security risk if you have whitespace, $ etc. in the command arguments, or the user has a shell config file. To avoid all such risks, use subprocess with a list or tuple of strings as the command (shell=False) instead.
To emulate os.chdir in the command, use the cwd= argument in subprocess.
This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Calling an external command in Python
I want to run commands in another directory using python.
What are the various ways used for this and which is the most efficient one?
What I want to do is as follows,
cd dir1
execute some commands
return
cd dir2
execute some commands
Naturally if you only want to run a (simple) command on the shell via python, you do it via the system function of the os module. For instance:
import os
os.system('touch myfile')
If you would want something more sophisticated that allows for even greater control over the execution of the command, go ahead and use the subprocess module that others here have suggested.
For further information, follow these links:
Python official documentation on os.system()
Python official documentation on the subprocess module
If you want more control over the called shell command (i.e. access to stdin and/or stdout pipes or starting it asynchronously), you can use the subprocessmodule:
import subprocess
p = subprocess.Popen('ls -al', shell=True, stdout=subprocess.PIPE)
stdout, stderr = p.communicate()
See also subprocess module documentation.
os.system("/dir/to/executeble/COMMAND")
for example
os.system("/usr/bin/ping www.google.com")
if ping program is located in "/usr/bin"
Naturally you need to import the os module.
os.system does not wait for any output, if you want output, you should use
subprocess.call or something like that
You can use Python Subprocess ,which offers many modules to execute commands, checking outputs and receive error messages etc.
This question already has answers here:
Run a .bat file using python code
(9 answers)
Closed 6 years ago.
call python 'D:\chan.bat'
"set of python statements is stored in notepad and saved as .bat extension.
how to run these statements in python.what can be the syntax?"
I think this is usually done using the subprocess module:
from subprocess import call
call("D:\chan.bat")
However a normal call doesn't give you back much information. You might need the power of a Popen object:
from subprocess import Popen
Popen("D:\chan.bat")
Edit:
You might need to take out the single quotes for this to work.
"'D:\chan.bat'" -> "D:\chan.bat"
If you don't need to interact with the script, wouldn't this work?
import os
os.system("d:\\chan.bat")
I have no Windows box to test it.
Here is what I try on Mac OS
Dinh-Phams-MacBook-Pro:tmp dinhpham$ cat > t.bat
print "abc"
Dinh-Phams-MacBook-Pro:tmp dinhpham$
Dinh-Phams-MacBook-Pro:tmp dinhpham$ python t.bat
abc
Python interpreter does not care about .py extension
If you want to load .bat file as Python module, just use
imp.load_source(path_to_file)
Command can be passed to python as follows:
[avasal#avasal]$ python -c "print 'a' + 'b'"
ab
[avasal#avasal]$
In python --help you can see, -c cmd : program passed in as string (terminates option list), In your batch file, you can make use of this option.