Search code examples
pythoncommand-promptsubdirectory

python text file in folder within the directory folder not found


I'm running my code in command prompt from the directory *FOLDER "Game" within "Game" I have other FOLDERS to keep it neat and easier to navigate/find files, inside a folder "Text" I have a Text file named "Note8.txt" so the text file "Note8.txt" is in folder "Text" inside the directory folder "Game" but while running my code the command prompt states error could not find "Note8" in the directory I took "Note8.txt" out of its folder and it worked, I assumed having folders within the directory folder would automatically make them subfiles so I could still use

with open("Note8.txt",'r') as Note8txt:
    Note8 = read(Note8txt.read())

this works fine with the text file in the folder but not when the text files are inside the folder, is there a way to get this to work or dose all my files need to be inside the folder and not inside folders within the folder?folders within the directory folder


Solution

  • Try This

    with open('TextFiles/Note8.txt', encoding='utf-8', mode='r') as file:
        print(file.read())
        file.close()
        # OR SOME OTHER CODE