1

I'm trying to make a basic text adventure game as shown below:

import time

print ("Hello and welcome to Prison Break")                           
print()                                                                            
print("Press S to start")                                                          
while True:
    choice = input("> ")

    if choice == 'S'or's' :                                                     
        print("Paddy Games presents...")
        time.sleep (2)
        print("Prison Break!")
        time.sleep(1)
        print("Before we begin, lets find out a bit about how you got 
        stuck in this prison in the first place!")
        time.sleep(2.5)
        print("When you are finished reading, type finished")
        file = open("Prison Break Backstory.txt","r")
        file_contents = file.read()
        print (file_contents)
        print()

The problem is that i get this when I go to run it: 'FileNotFoundError: [Errno 2] No such file or directory' I have checked that i have written the files name correctly, it is definitely there

Now I am aware that there are already solutions for this on the site using the exact, or absolute, path. However, I will be working on this at home on my raspberry pi 3 and also on my schools computers. The file will not be in the same place when i distribute the code. So in conclusion I need a solution that will make the file locatable on all computers regardless of where it is as long as it is there.

Sorry if this is a stupid question, I am still learning python and haven't perfected it yet. Thank you in advance for any replies!

Pr Music
  • 13
  • 1
  • 7
  • 1
    "So in conclusion I need a solution that will make the file locatable on all computers regardless of where it is as long as it is there." You want your program to search through every folder in the computer for this file? This seems a little unnecessary. You don't need an absolute path, rather just a relative path. Make sure this file is either located in the same folder as your python file, or make its position constant, relative to the python file. – Arthur Dent Mar 19 '18 at 19:18
  • Thanks, like i said i'm fairly new to python so could you please give me an example of what that would look like in code if you have the time? – Pr Music Mar 19 '18 at 19:38
  • import os # the OS module has all sorts of neat stuff. I still learn a lot reading its documentation. absolute_expected_path = "c:/my_files/Prison Break Backstory.txt" if os.path.exists(absolute_expected_path): print_file_contents(absolute_expected_path) elif os.path.exists(os.path.basename(absolute_expected_path)): print_file_contents(absolute_expected_path) else: print("Could not find introductory test.") – aschultz Aug 01 '19 at 21:36
  • Also you mentioned you were a beginner, and you may have learned a bit more by now, but just in case... if choice.lower()='s' is an easier way of doing what you want. In fact, your if statement sees (if choice == 'S') or ('s') and the second statement always evaluates to true. – aschultz Aug 01 '19 at 21:37

1 Answers1

1

Do you want an example of the relative path, or searching every file in the computer? For the latter, look at this question, so you could do something like:

for root, dirs, files in os.walk("C:/Users", topdown=False):
    for name in files:
        if name == "Prison Break Backstory.txt":
            file = open(os.path.join(root, name), "r")

But this is so incredibly inefficient I really recommend you not do this. Plus, if there happened to be two versions of this file located in different directories, it would screw stuff up.

Instead, what you should do is ensure you always know where this text file is located, relative to your python code. Say your entire project is located in C:/Users/myname/Desktop/Project, you could have your python code in C:/Users/myname/Desktop/Project/src (source), and your text file in C:/Users/myname/Desktop/Project/txtfiles. Whenever you send the "Project" folder to someone, the python code could access the text file like so:

file = open("../txtfiles/Prison Break Backstory.txt","r")

Also, make sure you always close the file at the end. It would probably be better to instead use

with open("../txtfiles/Prison Break Backstory.txt", "r") as f:
    file_contents = f.read()

So you don't have to close the file/ risk not closing it and getting i/o issues.

Arthur Dent
  • 356
  • 5
  • 13