Vedr: What does """ means in python?
Duncan Booth
duncan.booth at invalid.invalid
Mon Feb 10 05:50:52 EST 2014
Gisle Vanem <gvanem at yahoo.no> wrote:
> Regrading handy uses of ''', you learned me one trick when using Pythonÿ
> code in a Windows .bat file:
>
> 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.
>
A better trick would be to use a Powershell script instead of a batch
file:
-------------------------------------------
filter python() { $_ | c:\Python33\python.exe ($args -replace'(\\*)"','$1$1\"') }
Write-Host "This is the powershell script"
dir cert: | convertto-json | python -c @"
import json, sys
stores = json.loads(sys.stdin.read())
print("This is Python")
for store in stores:
print("{}: {}".format(store['PSChildName'], ', '.join(store['StoreNames'])))
print("Python done")
"@
Write-Host "All done"
-------------------------------------------
C:\scripts> . .\Pythoncerts.ps1
This is the powershell script
This is Python
CurrentUser: Root, UserDS, Disallowed, Trust, My, TrustedPublisher, SmartCardRoot, TrustedPeople, ADDRESSBOOK, AuthRoot,
McAfee Trust, CA, REQUEST, ACRS
LocalMachine: Disallowed, Trust, CA, TrustedPublisher, SmartCardRoot, My, TrustedPeople, AuthRoot, TrustedDevices, Root
Python done
All done
C:\scripts>
Notes on the above:
Powershell messes up arguments when running legacy programs. The filter
ensures that all arguments pass through Windows command line processing
unscathed (except they can't contain null characters). You don't actually
have to use the filter if you are careful about how you write quotes in the
code, but it makes life simpler.
Python scripts up to just over 32,000 characters can be written on the
command line this way. You can also assign the script to a variable and
keep the Python command a bit cleaner:
$script = @"
print("Python here!")
"@
python -c $script
Or without the filter it is best to avoid the double quotes:
$script = @"
print('Python here!')
"@
c:\python33\python.exe -c $script
To run from a traditional cmd.exe prompt you have to explicitly use
Powershell. The default file associations for .ps1 files will run notepad
instead.
If your system execution policy is Restricted (the default) use:
powershell -executionpolicy RemoteSigned .\Pythoncerts.ps1
Otherwise set the execution policy to something more lenient (at a
Powershell prompt running as administrator enter "Set-ExecutionPolicy
RemoteSigned") and you can just do:
powershell .\Pythoncerts.ps1
I also use Powershell interactively so I have the filters defined in my
startup ($Home\Documents\WindowsPowerShell\profile.ps1):
filter py() { $_ | py.exe ($args -replace'(\\*)"','$1$1\"') }
filter python() { $_ | c:\Python33\python.exe ($args -replace'(\\*)"','$1$1\"') }
--
Duncan Booth
More information about the Python-list
mailing list