Search code examples
pythonpython-2.7user-inputos.path

Checking Availability of user inputted File name


My program requires the user to input a Filename for a text file.

It then needs to check if the file already exists.

 else:
        FileName = input("Please input a Valid File Name : ")
        if os.path.isfile("C:/Users/Brads/Documents/", FileName, ".txt"):
            print("File Exists")
        else:
            print("File does not exist")

However it errors like this every time and i have no clue why.


    Traceback (most recent call last):
    File "C:/Users/Brads/Python/5.py", line 108, in 
    FileName = input("Please input a Valid File Name : ")
    File "", line 1, in 
    NameError: name 'Test' is not defined

i have tried

+str(FileName)+
which also results in the same error.

Any help is appreciated


Solution

  • In Python 2.x, input takes the user's input and attempts to eval it. You should use raw_input instead:

    fileName = raw_input("Please input a valid file name: ")
    # Here ----^