how can i run an easy python script and save it in a file but directly in linux command line:
fox#fox:/opt/gera# python -c print "aaaaa" > myfileName
but it is just print nothing instead of "aaaaa".
You have to quote the whole command:
python -c 'print "aaaaa"' > myfileName
Otherwise you execute print in Python (which, in Python 2 prints a linebreak and in Python 3 does nothing since you'd just evaluate the function print without calling it) and pass aaaaa as an argument to the script.
You need to put quotes around the code.
python -c 'print "aaaaa"' > myfileName
python -c 'print "aaaaa"' > myfileName
in your example python is running python -c print and giving "aaaaa" as an argument.
man python:
-c command
Specify the command to execute. This terminates the option list (following options are passed as arguments to the command).
passed as arguments to the command -- means everything after the command is available as sys.argv:
$ python -c 'import sys; print sys.argv' -a -b -c -d
['-c', '-a', '-b', '-c', '-d']
To make a single argument (command in terms of the man excerpt above) out of a list of arguments you just take these in quotes:
$ python -c 'print "aaaaa"'
aaaaa
Related
I am trying to do a string printing with python -c flag, e.g.
python3 -c "print('Hello World')"
So now I wanna substitute an argument with pipe, e.g. echo "Hello World" | python3 -c "print($1)"
the pipe is to take output from previous command and take it as input to next command, if I am not wrong, this is possible? But I think I got syntax error which I cannot find any source of this
I also bumped into question previously asked, but the solution required python imports and .py file depends on how we run this, I understand but I just wanna get it in a line of command in linux shell
If your input is always single line then you should be able to harness input function for example
echo "Hello World" | python3 -c "print(input().upper())"
would output
HELLO WORLD
I am trying to access a python function from the command line, and I would like to write such a command that will print the output in the terminal. The below doesn't work. What could I change?
python -c 'from laser import Laser; laser = Laser();l = laser.embed_sentences("hello", lang = "en").shape == (1, 1024); print(l)'
(base) ~ % python -c 'print("hello, world")'
hello, world
Printing works fine for me when running python through python -c. Are you sure your terminal isn't truncating your output by omitting the last (and in this case, only) line? You could try creating a single line file (no newline at the end) and then running cat [filename] (which is how I sometimes discover that my terminal is doing this)
-c cmd : program passed in as string (terminates option list)
That is the correct flag to be used. This must be a CLI config issue. Or the script is taking longer than you are expecting to run and it appears no output is generated.
Does python -c 'print("hello")' work?
A single command passed to Python interpreter via '-c' option works perfectly:
$ python3 -c "print('Hi')"
Hi
$
However, I couldn't figure out how to send multiple lines (from the Windows command prompt), since the statements are grouped by indentation. Passing multiple lines in a single line will not work.
A Linux terminal supports multiple lines with newline character as argument:
$ python3 -c "
>import sys
>print(sys.argv[0])"
$ -c
But in Windows it is not possible because the command get terminated with a newline
$ python3 -c "
$
How do I make this work in the Windows command prompt?
I am just checking out the options of Python interpreter, so I am not looking for any workaround solution!
You could use the ^ operator here. Something like
C:\>python3 -c "print('Hai')"
Hai
C:\>python3 -c "import sys; print(sys.argv)"
['-c']
C:\>python3 -c ^
More? "import sys; ^
More? print(sys.argv)"
['-c']
And,
C:\>python3 -c ^
More? "if 2*2 == 4: ^
More? print('Testing')"
Testing
And,
C:\>python3 -c ^
More? "if True: ^
More? print('First Line'); ^
More? print('Second Line')"
First Line
Second Line
I'm trying to have python generate the input parameter to my command line program (Linux), and simply cannot get it to work.
I know it is something to the effect of
./heap0 (python -c 'print "A"*72)
but that does not work....
Try $(). It takes the output of a command and includes it as a value.
./heap0 $(python -c 'print "A"*72')
Quoting from docs.python.org:
"sys.argv The list of command line arguments passed to a Python script. argv[0] is the script name (it is operating system dependent whether this is a full pathname or not). If the command was executed using the -c command line option to the interpreter, argv[0] is set to the string '-c'. If no script name was passed to the Python interpreter, argv[0] is the empty string."
Am I missing something, or sys.argv[0] always returns the script name, and to get '-c' I'd have to use sys.argv[1]?
I'm testing with Python 3.2 on GNU/Linux.
No, if you invoke Python with -c to run commands from the command line, your sys.argv[0] will be -c:
C:\Python27>python.exe -c "import sys; print sys.argv[0]"
-c
When Python is invoked as python script.py then sys.argv[0] == 'script.py'. When you invoke python -c 'import sys; print sys.argv' then sys.argv[0] == '-c' indicating the script body was passed as a string on the command line.
python -c executes a command passed on the command line, rather than a script from a file. sys.argv[0] will be set to "-c".
If you run a script with a -c flag, then yes, sys.argv[1] will be set to "-c" and sys.argv[0] will be set to the name of the script.