Search code examples
pythonfile-not-found

'FileNotFoundError: [Errno 2] No such file or directory' but can't use exact, or absolute, path


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!


Solution

  • 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.