没有输出`python.exe -c'print("hello")'`

Mis*_*ble 6 python python-2.7

我正在尝试学习python并偶然发现了我自己的另一个愚蠢的错误.

对于我从python.org下载的2.7.3版本,我没有获得任何简单程序的输出-c.我得到与Cygwin的2.6.8版本的输出.

我错过了什么?

> c:\Python27\python.exe --version
Python 2.7.3

> c:\Python27\python.exe -c 'print("hello")'

> c:\Python27\python.exe
Python 2.7.3 (default, Apr 10 2012, 23:24:47) [MSC v.1500 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> print("hello")
hello
>>> exit()

> c:\cygwin\bin\python2.6.exe --version
Python 2.6.8

> c:\cygwin\bin\python2.6.exe -c 'print("hello")'
hello

> c:\cygwin\bin\python2.6.exe
Python 2.6.8 (unknown, Jun  9 2012, 11:30:32)
[GCC 4.5.3] on cygwin
Type "help", "copyright", "credits" or "license" for more information.
>>> exit()
Run Code Online (Sandbox Code Playgroud)

Mat*_*dge 5

尝试没有程序周围的单引号:

python -c print(\"hello\")
Run Code Online (Sandbox Code Playgroud)

使用单引号,我猜它将输入解释为字符串,因此不进行打印.您还需要在程序本身中转义双引号.

编辑:

您不需要转义单引号,因此您可以这样做:

python -c print('hello')
Run Code Online (Sandbox Code Playgroud)

要么

python -c "print('hello')"
Run Code Online (Sandbox Code Playgroud)

(这是原始示例,只是交换了报价类型)

  • AARGH.问题不在于python,而是我忘记了cmd.exe的工作原理.我会在2分钟内接受这个. (2认同)