0

I want to use python as a pipe in the command line mode. I tried some ways like this:

ls -la | python -c "import sys; for line in sys.stdin: print(line[0])"

But it didn't work:

  File "<string>", line 1
    import sys; for e in sys.stdin: print(e[0])
                  ^
SyntaxError: invalid syntax

Any ideas?

Fomalhaut
  • 8,590
  • 8
  • 51
  • 95
  • How about this: `"import sys\nfor line in sys.stdin:\n\tprint(line[0])"`? – Ma0 May 17 '17 at 12:02
  • @Ev.Kounis It didn't work. `SyntaxError` – Fomalhaut May 17 '17 at 12:02
  • 2
    I was able to get it to work with this syntax - `python -c $'import sys;\nfor line in sys.stdin: print(line[0])'`. I had help from [this post](http://stackoverflow.com/a/23301247/558021). Note the addition of `\n` and the `$` character. – Lix May 17 '17 at 12:04
  • you can use also exec: `ls -la | python3 -c 'exec("import sys\nfor e in sys.stdin:\n\tprint(e[0])")'` – zelenyjan May 17 '17 at 12:14

0 Answers0