出于某种原因,尽管文件在主目录和同一目录中,我仍然得到没有这样的文件错误。任何帮助表示赞赏。
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)
答案 1 :(得分:0)
with open("Questions1.txt", "r") as f:
file_data = f.read().splitlines()
for line in file_data:
#do what ever you want
答案 2 :(得分:0)
最简单的解决方案归结为范式选择,请求许可或请求原谅。
请求许可:在使用
之前检查文件是否存在import os.path
if os.path.isfile("Questions1.txt"):
#read file here
请求宽恕:try-except block,报告问题
try:
#read file and work on it
except:
print 'Error reading file'
如果你使用读写标志,它会在它不存在的情况下创建一个文件,但它似乎并不像你想写的那样
with open("Questions1.txt", "rw") as f:
#work on file
选择你的毒药。希望这会有所帮助:)