2016-09-28 385 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 

选择你的毒药。希望这会有所帮助:)

相关问题