'indent'ing Python in windows bat
Duncan Booth
duncan.booth at invalid.invalid
Thu Sep 20 09:35:35 EDT 2012
Jason Friedman <jason at powerpull.net> wrote:
>> I'm converting windows bat files little by little to Python 3 as I
>> find time and learn Python.
>> The most efficient method for some lines is to call Python like:
>> python -c "import sys; sys.exit(3)"
>>
>> How do I "indent" if I have something like:
>> if (sR=='Cope'): sys.exit(1) elif (sR=='Perform') sys.exit(2) else
>> sys.exit(3)
>
> Some months ago I posted what I think is a similar question in the
> Unix world: I wanted to call a small portion of Python from within a
> Bash script.
>
> Someone on this list answered (for Bash):
>
> #!/bin/bash
> command1
> command2
> python -c "if True:
> import module
> if condition:
> do_this
> else:
> do_that
> "
> command4
> # end code
>
> Perhaps something similar would work for a .bat file.
>
Provided there's only one Python block in the .bat or .cmd file then
there's no real problem; you just have to hide each language from the
other:
goto :start
"""
:start
@echo off
echo This is a CMD script
python -x %~f0 "%1"
echo Back in the CMD script
goto :eof
"""
import sys
print("Welcome to Python")
print("Arguments were {}".format(sys.argv))
print("Bye!")
You can put the Python code either before or after the triple-quote
string containing the CMD commands, just begin the file with a goto to
skip into the batch commands and end them by jumping to eof.
--
Duncan Booth http://kupuguy.blogspot.com
More information about the Python-list
mailing list