calling python script in batch file..

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
adeline
Posts: 3
Joined: 14 Jun 2012 02:45

calling python script in batch file..

#1 Post by adeline » 17 Jun 2012 19:43

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.

Fawers
Posts: 187
Joined: 08 Apr 2012 17:11
Contact:

Re: calling python script in batch file..

#2 Post by Fawers » 17 Jun 2012 22:26

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.
Last edited by Fawers on 17 Jun 2012 22:33, edited 1 time in total.

phillid
Posts: 109
Joined: 03 Apr 2010 20:27
Location: Wellington, New Zealand
Contact:

Re: calling python script in batch file..

#3 Post by phillid » 17 Jun 2012 22:32

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

Fawers
Posts: 187
Joined: 08 Apr 2012 17:11
Contact:

Re: calling python script in batch file..

#4 Post by Fawers » 17 Jun 2012 22:37

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.

adeline
Posts: 3
Joined: 14 Jun 2012 02:45

Re: calling python script in batch file..

#5 Post by adeline » 18 Jun 2012 02:53

hi both,

thanks.It worked :)

Post Reply