Blaaaakuu Blaaaakuu - 6 months ago 16
Python Question

input function does not work & returns FileInput object

I'm trying to get user input as function call in a small python program but, when i'm using the

method, python shell does not let me input anything and quit by itself.

from fileinput import input
def funcinput():
comm = input("-->")
print('your input is %s' %comm)

if __name__ == "__main__":
print("try to get input")

Code looks like this and every time I run it, python launcher just give me:

try to get input
your input is <fileinput.FileInput object at 0x10464c208>

without let me input anything

What should I do to make it work?


The cause here is a masking of the built-in input. In some point, a from fileinput import input occurs, which masks builtins.input and replaces it with fileinput.input. That function doesn't return an str but, instead, returns an instance of FileInput:

from fileinput import input

print(input("--> "))
<fileinput.FileInput at 0x7f16bd465160

If importing fileinput is necessary import the the module and access input with dot notation:

import fileinput

print(fileinput.input("--> ")) # fileinput input, returns FileInput instance.
print(input("--> "))           # original input, returns str

If this option is failing, you could try and del input to get the look-up for it to use the built-in version:

>>> from fileinput import input
>>> print(input("--> "))
<fileinput.FileInput at 0x7f16bd465f60>
>>> del input
>>> input("--> ")