how to run a .bat file in python? [duplicate] - python

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.

Related

How do we create alias from python program/script? [duplicate]

This question already has an answer here:
create unix alias using a python3 script
(1 answer)
Closed 2 years ago.
I want to create an alias' through python program. I intend to use python as a replacement for shell scripting.
What I tried is:
import os
os.system('alias go2dir="cd /i/want/to/goto/this/dir"')
...and it does not work. I know the reason - that system command 'alias...' is getting executed in another shell and not in the current one where this python script is executed. So, that alias is not available to this shell.
What I don't know is - (In general,) how do we execute a command from a python program in the same shell where this python program is being executed. So that (in this case) the alias is available till the shell terminal is open?
The way other applications that want to automate actions in the user's shell work is that they write shell commands to their standard output. Then you can execute them with eval.
makealias.py:
print('alias go2dir="cd /i/want/to/goto/this/dir"')
Then in bash:
eval "$(python makealias.py)"
An example of a standard Unix program that works like this is tset with the -s option.

Python - Code does not run without #!/usr/bin/python [duplicate]

This question already has answers here:
Why do people write #!/usr/bin/env python on the first line of a Python script?
(22 answers)
Closed 7 years ago.
Okay I am new to Python, but my code does not run if the line
#!/usr/bin/python
is not present at the beginning of the file. Why is that ? What does it mean ? I thought it was used for to denote a python version if there were multiple versions installed.
#!/usr/bin/python
def main():
a = [1,2,3]
print a
if __name__ == "__main__":
main()
Omitting the #!/usr/bin/python gives the following error only if I execute it using
./test.py on Ubuntu
if however I use the python command to run then it runs fine without the /usr/bin line.
The first line of the script beginning with #! is a shebang (sometimes called a hash-bang).
The following executable path denotes which interpreter should be used to process the following code (in your case, /usr/bin/python).
If you run the script from the shell with python test.py, you don't need a shebang - the executable is python and the script is passed to it as an argument.
In Unix you can tell a file how it should be opened if it contains a script language (in your case Python).
This line is known as Shebang.
./filename is used to run executable files, to execute it you need to specify the application required.
Whereas, in using python filename.py you already specify the application to use that is python in this case.

Running shell commands within python interpreter [duplicate]

This question already has answers here:
How do I execute a program or call a system command?
(65 answers)
Closed 8 years ago.
Is there a simple method for calling shell command line arguments (like ls or pwd) from within python interpreter?
In plain python, you need to use something along the lines of this:
from subprocess import check_output
check_output("ls", shell=True)
In IPython, you can run either of those commands or a general shell command by starting off with !. For example
! echo "Hello, world!" > /tmp/Hello.txt
If you're using python interactively, you would almost certainly be happier with IPython.
If you meant to use the Python shell interactively while being able to call commands (ls, pwd, ...) check out iPython.

run python program one by one

Is possible to run bunch of python based program one by one in python terminal? I have number of program that is executed in Python. I want to make a single file, so that I can just run a single python file, and it will execute each program one by one in that file.
Single file:some_files.py
\home\something\1.py
\home\something\2.py
\home\something\3.py
\home\something\4.py
There are two very easy ways to achieve the same goal, without using a python script nor python terminal (I'm aware that's not exacly what you're asking, but it's very easy).
An IPython script
run_all.ipy (.ipy is the extension for ipython scripts)
%run \home\something\1.py
%run \home\something\2.py
%run \home\something\3.py
%run \home\something\4.py
A shell/batch script
(on windows use a batch file (.bat) instead)
run_all.sh
python \home\something\1.py
python \home\something\2.py
python \home\something\3.py
python \home\something\4.py
You can use the suprocess build-in library.
import subprocess
prog_max = 10
for i in range(prog_max):
s = subprocess.Popen(['python','%i.py'%i], stdout=subprocess.PIPE, stderr=subprocess.STDOUT, shell=True)
out,_ = s.communicate()
I use more simple approch in Subprocess module,
import os
os.chdir('\home\something\')
import subprocess`
subprocess.call(['python','1.py'])
subprocess.call(['python','2.py'])

Shell expansion in subprocess? [duplicate]

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)

Categories

Resources