2016-02-27 60 views
-2

是幾周齡編碼器不打開文本文件時,我無法使Python閱讀我的文件,儘管代碼和.txt文件在同一文件夾Python的閒置與開放()

highest_score=0 
result_f = open("results.text") 
for line in result_f: 
    if float (line)>highest_score: 
     highest_score = float(line) 
     result_f.close() 
     print("the highest score:") 
     print(highest_score) 

和結果是是

Traceback (most recent call last): 
File "C:\Python33\-mu.py", line 2, in <module> 
result_f = open("results.text") 
FileNotFoundError: [Errno 2] No such file or directory: 'results.text' 

請幫

+0

顯然,該文件不存在於**執行腳本的目錄中。 – sjaustirni

+0

一個誠實的錯誤,容易做出。或者cd到您的文本文件所在的文件夾,或者在打開時包含整個路徑() –

回答

0

基本上你試圖打開該文件result.text。返回的錯誤是Python空閒無法找到該文件。我注意到您提到了一個.txt文件,但在您的代碼中,您嘗試打開.text文件。 所以我建議你檢查一下你的文件擴展名爲.txt.text,以防萬一。

如果錯誤仍然存​​在,請嘗試提供打開命令的完整路徑。例如:

result_f = open("/Users/Joe/Desktop/results.text") 

Windows和Mac OS給予,在文件上某處時,右鍵單擊,完整路徑(以及其它信息)。

+0

您是對的,我輸入了錯誤的擴展名。與.txt,它的工作,謝謝:) – Farhan