Python -c bash swtich newline - python

I'd like to run the command:
print "hello"
print "world!"
from python with the -c switch from bash, something like this:
python -c 'print "hello" \n print "world"'
But I can't figure out what the correct newline character should be in the string after the -c switch.

How about:
python -c 'print "hello"; print "world"'

some python statements cannot be written in one line, so I recommend this:
python -c '
print "hello"
print "world"
'
in this way statements such as for can be appeared.

python -c 'print "hello\nworld"'

Related

Use Python to split a string and output each token into seperate lines?

$ echo '"a1","a2","a3"'|python3 -c "import sys; print('\n'.join(sys.stdin.read().splitlines()), sep='\n');"
"a1","a2","a3"
$ echo '"a1","a2","a3"'|python3 -c "import sys; [print(a, sep='\n') for a in sys.stdin.read().splitlines()];"
"a1","a2","a3"
$ echo '"a1","a2","a3"'|python3 -c "import sys,pprint; pprint.pprint('\n'.join(sys.stdin.read().splitlines()));"
'"a1","a2","a3"'
I have tried many different methods but none of them work for me.
I would like to print each token into a seperate line.
Question> How can I get the following results?
"a1"
"a2"
"a3"
Thank you
Split on comma instead.
print('\n'.join(sys.stdin.read().split(',')))

Multiline python code execution in terminal (Windows)

As the question states, I would like to know how to execute arbitrary python code in a windows terminal (or Anaconda Prompt). Please note that I am on Windows. In general, terminal execution is something like:
>>python -c "print('Some Text to Print')"
But what happens if I have multiple lines with indents and indent-indents. Here is an example of things I have tried (please note that I am forcing two lines here in some way, the solution "for i in range(10): print(i)" is not appropriate to the question since this only has one line of python code):
>>python -c "for i in range(10):; print(i)"
>>python -c "for i in range(10):\nprint(i)"
>>python -c "for i in range(10):\n print(i)"
>>python -c "for i in range(10):\n\tprint(i)"
Now clearly I can do this:
>>python -c "exec('for i in range(10):\n\tprint(i)')"
and this solution works. But this solution feels wrong. Is there a more pythonic way? In particular, why do the newline and tab characters work for "exec()" but not for "python -c"?
Have you already tried with ^ ?
In this case if you will not be careful with spaces, you will obtain an IndentationError: unexpected inden
python -c ^
More? "for i in range(2): ^
More? print(i)"
[EDIT]
There are also another way:
python -c ^
More? "for i in range(2):^ print(i)"
0
1
^ is the EOL character used as the new line character in most other non-Unix operating systems, including Microsoft Windows and Symbian OS.
You can do
> echo "for i in range(2):
>> print(i)
>> " | python
0
1

Invoke method of python class from groovy script

I want to execute a method of my python class from groovy script.
This method have two parameter.
When i execute this command from terminal:
python -c 'import Myclass; Myclass.method("param1","param2")' it is working.
I use this groovy script code :
def cmd = "cd /path/to/the/folder && python -c 'import Myclass; Myclass.method(param1,param2)'"
def proc = ["/bin/sh", "-c", cmd].execute()
proc.waitFor()
println "return code: ${proc.exitValue()}"
println "stderr: ${proc.err.text}"
println "stdout: ${proc.in.text}"
When i want to do the same in the groovy script i have error with the parameter : NameError: name 'param1' is not defined.
Do you have any idea why ?
Best regards
When you executed the script in the terminal, you used the string literals "param1", "param2" , not the undefined variables param1, param2. Since you have already used both single and double quotes, you should escape the double quotes with a backslash:
def cmd = "cd /path && python -c 'import Myclass; Myclass.method(\"param1\", \"param2\")'"
or, just use the triple quotes:
def cmd = '''cd /path && python -c 'import Myclass; Myclass.method("param1", "param2")' '''

Using bash echo command to input into a python one liner

I am trying to input a string using echo into a Python one liner then perform a Caeasar's Cipher on the string.
One of the examples my instructor gave me was this.
~ $ echo "Hello Holly." | python -c "import sys; [print(line) for line in sys.stdin]"
The output is suppose to be: Hello Holly.
How ever when I type the command in I get:
File "<string>", line 1
import sys; [print(line) for line in sys.stdin]
^
SyntaxError: invalid syntax
I would appreciate it if someone could point out the error to me. I am using Python 2.6 on Centos 6.
Thanks.
In Python 2 print is a statement, not a function. Try this instead:
echo "Hello Holly." | python -c "import sys; print [line for line in sys.stdin]"
Alternatively, you could use the following to just print plain text (thanks #mgilson):
echo "Hello Holly." | python -c "import sys; print ' '.join([line for line in sys.stdin])"
It looks like you are using python2 while your isntructor is using python 3.

Python one-liner to print every file in the current directory

How can I make the following one liner print every file through Python?
python -c "import sys;print '>>',sys.argv[1:]" | dir *.*
Specifically would like to know how to pipe into a python -c.
DOS or Cygwin responses accepted.
python -c "import os; print os.listdir('.')"
If you want to apply some formatting like you have in your question,
python -c "import os; print '\n'.join(['>>%s' % x for x in os.listdir('.')])"
If you want to use a pipe, use xargs:
ls | xargs python -c "import sys; print '>>', sys.argv[1:]"
or backticks:
python -c "import sys; print '>>', sys.argv[1:]" `ls`
You can read data piped into a Python script by reading sys.stdin. For example:
ls -al | python -c "import sys; print sys.stdin.readlines()"
It is not entirely clear what you want to do (maybe I am stupid). The confusion comes from your example which is piping data out of a python script.
If you want to print all files:
find . -type f
If you want to print only the current directory's files
find . -type f -maxdepth 1
If you want to include the ">>" before each line
find . -type f -maxdepth 1 | xargs -L 1 echo ">>"
If you don't want the space between ">>" and $path from echo
find . -type f -maxdepth 1 | xargs -L 1 printf ">>%s\n"
This is all using cygwin, of course.
ls | python -c "import sys; print sys.stdin.read()"
just read stdin as normal for pipes
would like to know how to pipe though
You had the pipe the wrong way round, if you wanted to feed the output of ‘dir’ into Python, ‘dir’ would have to be on the left. eg.:
dir "*.*" | python -c "import sys;[sys.stdout.write('>>%s\n' % line) for line in sys.stdin]"
(The hack with the list comprehension is because you aren't allowed a block-introducing ‘for’ statement on one line after a semicolon.)
Clearly the Python-native solution (‘os.listdir’) is much better in practice.
Specifically would like to know how to pipe into a python -c
see cobbal's answer
piping through a program is transparent from the program's point of view, all the program knows is that it's getting input from the standard input stream
Generally speaking, a shell command of the form
A | B
redirects the output of A to be the input of B
so if A spits "asdf" to standard output, then B gets "asdf" into its standard input
the standard input stream in python is sys.stdin

Categories

Resources