2012-10-18 151 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()