Page 1 of 1

calling python script in batch file..

Posted: 17 Jun 2012 19:43
by adeline
hi,

i just develop a python script and wondering how to call it in batch file so that i can automate in schedule task.

say,

in cmd i need to type in python.py <input.txt> <output.txt>

but if i write in batch file, how shall i go with it say python.py cannot be recognize as batch file?

#i have installed python 3.2 already.

The requirement of the batch file is like

Code: Select all

@echo off
cd \
cd input     

for %%f in (*.txt) do (


   echo "%%~nf"
   "D:\impressio\deployment code\add_null.py" .\"%%~nf.txt" .\ %%~nf_processed.txt

)


if anyone know the solution.

Re: calling python script in batch file..

Posted: 17 Jun 2012 22:26
by Fawers
I'm studying Python so I think I might be able to help you. Could you post your python code?

Edit
Wait. Maybe your P code isn't necessary.

Try this:

Code: Select all

@echo off
cd \input
for %%f in (*.txt) do (
   echo "%%~nf"
   "D:\impressio\deployment code\add_null.py" "%%~f" ".\%%~Nf_processed.txt"
)

If it does what you want it to, you can remove the ECHO.

Re: calling python script in batch file..

Posted: 17 Jun 2012 22:32
by phillid
I'm not familiar with python myself, but there will be an interpreter called python.exe.

So I'm pretty sure something like...

Code: Select all

@echo off
cd \
cd input     

for %%f in (*.txt) do (
   echo "%%~nf"
   path/to/python.exe "D:\impressio\deployment code\add_null.py" .\"%%~nf.txt" .\ %%~nf_processed.txt
)

...would suffice.

I'm not sure if the arguments will be passed onto the script though, there will be a way if this doesn't work (maybe a switch?).

Give it a go anyway and get back to us :)

Thanks
Phillid

Re: calling python script in batch file..

Posted: 17 Jun 2012 22:37
by Fawers
phillid wrote:I'm not sure if the arguments will be passed onto the script though, there will be a way if this doesn't work (maybe a switch?).


As far as I know (and tested), arguments in Python don't need switches to be passed on. And, just like cmd, its first argument (0) is always the program full path.

Just thought I'd share that.

Re: calling python script in batch file..

Posted: 18 Jun 2012 02:53
by adeline
hi both,

thanks.It worked :)