2012-10-18 148 views
3

这是练习15从Learn Python的难题,但我使用Python 3Python 3 - 从文件中读取文本

from sys import argv 
script, filename = argv 

txt = open(filename) 
print ("Here's your file %r:") % filename 
print txt.read() 

print ("I'll also ask you to type it again:") 
file_again = input() 
txt_again = open(file_again) 
print txt_again.read() 

文件被保存为ex15.py,当我从终端运行它它读取ex15.txt正确第一次,但是当我请求它第二次,我得到一个错误

[email protected]:~/Desktop/python$ python ex15.py ex15.txt<br> 
Here's your file 'ex15.txt':<br> 
This is stuff I typed into a file.<br> 
It is really cool stuff.<br> 
Lots and lots of fun to have in here.<br> 

I'll also ask you to type it again:<br> 
ex15.txt <b>#now I type this in again, and I get a following error</b><br> 
Traceback (most recent call last):<br> 
    File "ex15.py", line 11, in <<module>module><br> 
    file_again = input()<br> 
    File "<<string\>string>", line 1, in <<module>module><br> 
NameError: name 'ex15' is not defined 

怎么了?

+0

你确定你在py3k上? –

+0

for python 3你需要使用'print()'函数而不是'print'语句和[str.format()](http://docs.python.org/library/stdtypes.html#str.format)而不是'%'=>你没有使用python 3,检查你的操作系统的'.py'文件的默认解释器 – Aprillion

+0

在print()上达成一致,但是你仍然可以在CPython 3.x中的字符串上使用%运算符。对于双版本代码,使用%可能是件好事。另外,pylint会针对类型问题进行检查。 – user1277476

回答

5

您的打印声明表明您没有像您在标题中所说的那样使用py3k。

print txt.read()这不适用于py3k,因此请确保您确实使用py3k。

您需要使用raw_input()而不是input(),因为您位于py 2.x

例如PY 2.X:

>>> x=raw_input() 
foo 
>>> x=input() # doesn't works as it tries to find the variable bar 
bar      
Traceback (most recent call last): 
    File "<stdin>", line 1, in <module> 
    File "<string>", line 1, in <module> 
NameError: name 'bar' is not defined 

例如PY 3.X:

>>> x=input() 
foo 
>>> x  # works fine 
'foo' 
+1

不,python 3没有raw_input。它被重新命名为'input' – Serdalis

+3

,但他的'print'声明表明他没有使用py3k。 –

+2

但他的标题和他的第一行说他正在使用python 3 – jdi

3

你似乎没有使用Python 3.0 .. 检查这是,只需要输入成终端窗口:

python

,并期待在信息琳es当interperator开始时出现。

Python 2.7.3 (default, Apr 10 2012, 23:31:26) [MSC v.1500 32 bit (Intel 
32 
Type "help", "copyright", "credits" or "license" for more information. 

它应该的方式是这样的,
python 2.7.3,并为python 3.X.X它会说python 3.X.X代替。

如果您使用的是Python 2.X,则Ashwini Chaudhary有正确的答案。

+0

他的'print txt.read()'实际上工作正常,所以他肯定不使用py3k。 –

+1

@AshwiniChaudhary你是对的:) – Serdalis

7

你肯定没有使用Python 3。有几件事情,使这个显而易见的:这是调用eval on input()这是

print ("Here's your file %r:") % filename 
print txt.read() 

print txt_again.read() 
    • 这些print声明没有括号(这需要在Python 3而不是2) changed in Python 3

      file_again = input() 
      

    最有可能的PY马拉松2是你的系统默认的,但你可以通过添加这是你的脚本的第一行(如果你直接运行它,就像./myscript.py)使你的脚本始终使用Python 3:

    #!/usr/bin/env python3 
    

    或者使用Python 3明确运行它:

    python3 myscript.py 
    

    另外一个注意事项:当你完成它时,你应该真的关闭文件。您可以做到这一点明确:

    txt = open(filename) 
    # do stuff needing text 
    txt.close() 
    

    或者使用with statement并有当块结束它的处理:

    with open(filename) as txt: 
        # do stuff needing txt 
    # txt is closed here 
    
  • +0

    +1为了解释不同版本之间的差异,OP可能真正使用哪个版本。 – jdi

    0

    试试这个代码,py3k:

    txt = open(filename, 'r') 
    print('Here\'s your file %r: ' % filename) 
    print(txt.read()) 
    txt.close() 
    
    print('I\'ll also ask you to type it again: ') 
    file_again = input() 
    txt_again = open(file_again, 'r') 
    print(txt_again.read()) 
    txt_again.close()