Why is the code giving a NameError when trying to taking user input for a file name and then reading from that file

25 Views Asked by At

the following Python code is to prompt user to specify a file name (which is stored in the variable 'file_name') and then read data from the file.

file_name = input("Enter the file name: ")

# Open the file and read the data
with open(file_name, "r") as file:
    lines = file.readlines()

When i run this and provide a file name, it gives a FileNotFoundError. The file I am using is in the same folder as the .py file so why is it not recognising the file? am i missing something?

I have tried moving the file to several locations such as the C: root directory but it still does not recognise it

0

There are 0 best solutions below