-3

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?"

4 Answers4

7

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"

machine yearning
  • 9,889
  • 5
  • 38
  • 51
3

If you don't need to interact with the script, wouldn't this work?

import os
os.system("d:\\chan.bat")
claesv
  • 2,075
  • 13
  • 28
  • Calling the program through the shell is usually not required; see http://docs.python.org/library/subprocess.html#replacing-os-system – machine yearning Mar 19 '12 at 08:06
2

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)
pcdinh
  • 666
  • 6
  • 8
0

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.

avasal
  • 14,350
  • 4
  • 31
  • 47