Dave Lee
Dave Lee

Reputation: 43

Python is stating my file is not in the directory even though it is saved on my computer. what is wrong?

Assume that a file containing a series of integers is named numbers.txt and exists on the computer’s disk. Write a program that calculates the average of all the numbers stored in the file.

I have a file name numbers_good.txt saved on my computer. When I type it in the error reads no file in directory.

def main():
    try:
        filename=input("name of the file")
        myfile=open(filename, "r")
    except IOError:
        print("File Error")

main()

Upvotes: 3

Views: 1711

Answers (1)

kirelagin
kirelagin

Reputation: 13616

This is most likely an issue with relative paths. Probably for some reason working directory for your program is not the one you expect.

Try this program to see, where Python is actually looking for your file.

import os.path

filename = input("name of the file: ")
print(os.path.abspath(filename))

You should either input absoulte path or move your file into the working directory (you can deduce it from the output of the program I posted).

Upvotes: 4

Related Questions