简体   繁体   中英

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 input() 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")
    funcinput()

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

Using input now picks up fileinput.input :

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

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

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

If this option is failing (or, you have no control of imports for some reason), you could try and delete the imported reference to get the look-up for it to use the built-in version:

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

Now by deleting the reference with del input :

>>> del input
>>> input("--> ")
--> 

input works as it always does.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM