1

My code doesn't seem to be able to find a text file I created. The .txt file is in the same folder as the .py-file named trivia.txt. It's from the Python programming for the absolute beginner. I get the following error:

>>> 
Unable to open the file trivia.txt Ending program
 [Errno 2] No such file or directory: 'trivia.txt'
Traceback (most recent call last):
  File "C:\Users\hugok\Desktop\Python programs\Trivia_game.py", line 64, in <module>
    main()
  File "C:\Users\hugok\Desktop\Python programs\Trivia_game.py", line 39, in main
    trivia_file = open_file("trivia.txt", "r")
  File "C:\Users\hugok\Desktop\Python programs\Trivia_game.py", line 8, in open_file
    sys.exit()
SystemExit
>>> 

This is the code I am trying to run:

import sys
def open_file(file_name, mode):
    try:
        the_file=open(file_name, mode)
    except IOError as e:
        print("Unable to open the file", file_name, "Ending program\n", e)
        input=("\nPress the enter key to exit")
        sys.exit()
    else:
        return the_file

def next_line(the_file):
    line=the_file.readline()
    line=line.replace("/", "\n")
    return line

def next_block(the_file):
    cathegory=next_line(the_file)

    question=next_line(the_file)

    answers=[]
    for i in range(4):
        answers.append(next_line(the_file))

    correct=next_line(the_file)
    if correct:
        correct=correct[0]

    explanation=next_line(the_file)

    return category, question, answers, correct, explanation

def welcome(title):
    print("\t\tWelcome to Trivia Challenge!\n")
    print("\t\t", title, "\n")

def main():
    trivia_file = open_file("trivia.txt", "r")
    title=next_line(trivia_file)
    welcome(title)
    score=0

    category, question, answers, correct, explanation=next_block(trivia_file)
    while category:
        print(category)
        print(question)
        for i in range(4):
            print("\t", i+1, answers[i])

        answer=input("What's your answer?")
        if answer==correct:
            print("\nRight!", end=" ")
            score+=1
        else:
            print("\nWrong!", end=" ")
        print(explanation)
        category, question, answers, correct, explanation=next_block(trivia_file)

    trivia_file.close()
    print("That was the last question")
    print("Your final score is", score)

main()

input("\n\nPress the enter key to exit.")
kiner_shah
  • 3,445
  • 6
  • 25
  • 36
Hugo Karlsson
  • 11
  • 1
  • 2

1 Answers1

0

Your .txt file being in the same folder as the .py file doesn't really mean anything. You need to make sure that the result of:

import os
os.getcwd()

is the directory where you have your text file. The traceback tells us that it is not. So you have two options here:

  1. Change your working directory at the top of the script to where your text file is:

    os.chdir("C:/Users/hugok/Desktop/Python programs")
    
  2. When you call open(), specify the full path to your text file:

    def main():
        open_file("C:/Users/hugok/Desktop/Python programs/trivia.txt", "r")
        ...
    

Either solution should work, please update us regarding the result once you apply them.

FatihAkici
  • 4,161
  • 1
  • 25
  • 47
  • Thank you. I still get the same error message. I had to change the backslash (\) to regular slash (/) though because of the sama problem described here: https://stackoverflow.com/questions/18084554/why-do-i-get-a-syntaxerror-for-a-unicode-escape-in-my-file-path – Hugo Karlsson Mar 11 '18 at 14:02
  • Oh of course, that change makes sense. Other than that, can you run this and let me know if your text file is in the result: `os.listdir(os.getcwd())` – FatihAkici Mar 11 '18 at 16:06
  • Hi again, sorry for the late reply. But yes the txt-file is in the result of that command: >>> os.listdir(os.getcwd()) ['Hanging_man.py', 'Numberguess.py', 'Numberguess_computer.py', 'pickles.dat', 'read_it.txt', 'skilltree.py', 'test.py', 'trivia.txt.txt', 'Trivia_game.py', 'write_it.txt'] – Hugo Karlsson Mar 24 '18 at 16:24
  • Well its name is what is confusing you. The name is 'trivia.txt.txt' right now. Go to the folder and rename the file to 'trivia'. That is, delete the .txt part from its name. – FatihAkici Mar 24 '18 at 19:06