Multi-line commands with 'python -c'

Duncan Booth duncan.booth at invalid.invalid
Fri May 30 17:11:37 EDT 2014


Chris Angelico <rosuav at gmail.com> wrote:

> Problem: Translate this into a shell one-liner:
> 
> import os
> for root, dirs, files in os.walk("."):
>     if len(dirs + files) == 1: print(root)
> 

This is one area where Windows seems to do better than Linux shells:

PS C:\python33> python -c "import os`nfor root, dirs, files in os.walk('.'):`n    if len(dirs + files) == 1: print(root)`n"
.\Doc
.\Lib\concurrent\__pycache__
.\Lib\curses\__pycache__
...

The `n shell escaped newline is interpreted well before Python runs.

Also the multiline version works and in Powershell ISE up-arrow pulls it back as a 
single unit for easy editing:

PS C:\python33> python -c @"
import os
for root, dirs, files in os.walk('.'):
    if len(dirs + files) == 1: print(root)
"@
.\Doc
.\Lib\concurrent\__pycache__
.\Lib\curses\__pycache__
... and so on ...


-- 
Duncan Booth



More information about the Python-list mailing list