I keep getting Error: "No such file or directory"

880 Views Asked by At

I have a program where I want to access a text file from my program. Even though the code is correct and the file has been created in the same directory, PyCharm is refusing to access it. I want to know how to fix this error.

I want to access a text file "data.txt" from my program called "bonus11". Even though the code is correct and the file has been created in the same directory, the program cannot open it. I have already tried pasting the entire file path.

    def get_average():
        with open("data.txt.py", 'r') as file:
            data = file.readlines()
        return data
    
    
    average = get_average()
    print(average)

This is code with the file and directory: This is code with the file and directory

I want to know how to access the file named data.txt from the bonus11. I would be thankful for any changes to the code

1

There are 1 best solutions below

0
Antoine On

The path is incorrect, it should be with open("./files/data.txt.py", 'r') as file: