Python 3.2.2从控制台读取

Python 3.2.2从控制台读取,python,console,python-3.x,Python,Console,Python 3.x,我正在研究Python 3.2.2。用户可以选择在控制台上输入值,或者如果他只是按ENTER键,则使用默认值。e、 g.如果用户点击ENTER键,该值将设置为c:\temp,如下面的代码段所示: READ=os.read(0,100) if READ == "\n" : READ="c:\\temp" 这段代码过去在Python2.7中工作,但在Python3.2.2中不工作 在3.2.2中,读数保持为空。 有任何改进此代码的建议吗?函数os.read在Python2.7中返回class

我正在研究Python 3.2.2。用户可以选择在控制台上输入值,或者如果他只是按ENTER键,则使用默认值。e、 g.如果用户点击ENTER键,该值将设置为c:\temp,如下面的代码段所示:

READ=os.read(0,100)
if READ == "\n" :
  READ="c:\\temp"
这段代码过去在Python2.7中工作,但在Python3.2.2中不工作

在3.2.2中,读数保持为空。
有任何改进此代码的建议吗?

函数
os.read
在Python2.7中返回
class str
,但在Python3.2中返回
class bytes
。因此,在Python3.2中,
if READ==“\n”:READ=“C:\\temp”
永远
True
。您可以这样更改:

if str(READ,"ascii") == os.linesep: READ = "C:\\temp"
也许,更确切地说:

import os,sys
READ = os.read(0,100)
if str(READ,sys.stdin.encoding) == os.linesep:
   READ = "C:\\temp"
Python 3已生成,因此
os.read()
返回二进制字符串

代码应该是固定的

if READ == b'\n' :
  READ="c:\\temp"

可能有助于说明为什么它不起作用……为什么
os.read(01100)
而不是仅仅
input()
?在这种情况下,您应该能够解决它;首先,
打印(repr(READ))
。谢谢。我把它改成了
os.linesep
。非常感谢。“如果str(读,“ascii”)==os.linesep”起作用。