pythoninput

Input data not defined


I am writing some code and need the user to input a file for use in the program:

file=input('input file name')

however, whenever you then input a file name (or anything for that matter) an error pops up saything that whatever has just been input is not defined and ends the program. What is causing this to happen?

Thanks


Solution

  • This is important:

    input([prompt]) -> value
    Equivalent to eval(raw_input(prompt)).
    

    Input will try to eval your input

    Check this

    In [38]: l =  input("enter filename: ")
    enter filename: dummy_file
    ---------------------------------------------------------------------------
    NameError                                 Traceback (most recent call last)
    C:\Python27\<ipython-input-37-b74d50e2a058> in <module>()
    ----> 1 l =  input("enter filename: ")
    
    C:\Python27\<string> in <module>()
    
    NameError: name 'dummy_file' is not defined
    
    
    In [39]: input /?
    Type:       builtin_function_or_method
    Base Class: <type 'builtin_function_or_method'>
    String Form:<built-in function input>
    Namespace:  Python builtin
    Docstring:
    input([prompt]) -> value
    
    Equivalent to eval(raw_input(prompt)).
    
    In [40]: file = raw_input("filename: ")
    filename: dummy_file
    
    In [41]: file
    Out[41]: 'dummy_file'
    

    using raw_input has it's disadvantages though.