On Thu, Oct 11, 2012 at 11:16 PM, D'Arcy J.M. Cain <da...@druid.net> wrote:
> On Thu, 11 Oct 2012 13:24:22 +0200
> Gisle Vanem <gva...@broadpark.no> wrote:
>
>> Hello list. I'm a newbie when it comes to Python.
>>
>> I'm trying to turn this:
>>
>>  def print_sys_path():
>>     i = 0
>>     for p in sys.path:
>>       print ('sys.path[%2d]: %s' % (i, p))
>>       i += 1
>>
>> into a one-line python command (in a .bat file):
>
> Is "one liner" an actual requirement or is the requirement to run it
> from the command line?
>
> python -c "
> import sys
> i = 0
> for p in sys.path:
>   print('sys.path[%2d]: %s' % (i, p))
>   i+=1
> "
>
> I don't know if this works on Windows or not.

It doesn't, I just tested it. Windows batch is appallingly crude
compared to a modern Unix shell; you may be able to find a way to get
around this, but the easiest solution for most batch files is going to
be an actual Python script file. You may be able to overlay your batch
and Python scripts with a trick like this:

rem = '''
@echo off
echo This is batch
\python32\python %0
echo All done
exit /b
rem '''
import sys
print("This is Python")
for i,p in enumerate(sys.path):
        print('sys.path[%2d]: %s' % (i, p))
print("Python done")

You'll have a variable in Python called 'rem' which contains all your
batch code :) It exploits the fact that 'rem' makes a one-line
comment, but the triple quotes go across multiple lines. (The "exit
/b" should exit the batch script without closing cmd.exe - this is yet
another weird WEIRD wart in Windows batch. I'm pretty sure neither DOS
nor OS/2 batch required that parameter.)

ChrisA
-- 
http://mail.python.org/mailman/listinfo/python-list

Reply via email to