2016-09-28 387 views
0

由於某些原因,儘管在主目錄和同一目錄中有文件,我仍然收到沒有這樣的文件錯誤。任何幫助表示讚賞。沒有這樣的文件或目錄?

import time 
    def firstQues(): 

      print('"TT(Announcing): HONING IN FROM SU CASA CUIDAD, (Their hometown)"') 
      time.sleep(3) 
      print('"VEN EL UN.....(Comes the one.....)"') 
      time.sleep(2) 
      print('"EL SOLO......(The ONLY.....)"') 
      time.sleep(3) 
      print('"Campeón NOVATO (NEWBIE CHAMP)"') 
      print() 


      text_file = open("Questions1.txt", "r") 
      wholefile = text_file.readline() 
      for wholefile in open("Questions1.txt"): 
        print(wholefile) 
        return wholefile 
        return text_file 

    def main(): 
      firstQues() 
      text_file = open("Questions1.txt", "r") 
    main() 
+0

如果不先關閉它,則無法打開同一個文件三次;有兩個返回語句也沒有意義,只有第一個返回語句會起作用。 –

+0

請檢查「Questions1.txt」和上面的python腳本是否在同一個文件夾中 –

+0

它們位於同一個文件夾中。 –

回答

0

無法以讀取模式打開不存在的文件。確保你在當前工作目錄中創建一個文件。您的程序已成功執行。

enter image description here

+0

好的,謝謝,它在正確的目錄中,但現在只是在將text_file.readline()更改爲text_file.read() –

+0

時只讀取文件的一行。沒關係,也解決了這個問題,感謝您的幫助。 –

0

最簡單的辦法歸結爲一種模式的選擇,請求許可請求寬恕

請求權限:檢查文件是否使用

import os.path 

if os.path.isfile("Questions1.txt"): 
    #read file here 

請求寬恕之前就存在:try-except塊,報告,如果問題

try: 
    #read file and work on it 
except: 
    print 'Error reading file' 

如果使用讀寫標誌,它將創建一個文件時,它不存在,但它似乎並不像你想寫

with open("Questions1.txt", "rw") as f: 
    #work on file 

選擇你的毒藥。希望這會有所幫助:)

相關問題