-3
def get_key_file():
    '''() -> file open for reading

       Return the key file.
    '''    

    return open(input("Enter the name of the key file: "), 'r')

我想编写另一个函数来返回用户在提示中输入的任何内容

4

2 回答 2

0

尝试在它自己的变量中捕获 input() 的返回值。然后您可以打印该值并将其用于打开。如果您需要能够在其他地方使用文件名,您可以同时返回 open() 的结果和 input() 的结果,如下所示:

def openFile():
    fileName = input("Enter the name of the key file: ")
    fileHandle = open(fileName, 'r')
    return fileName, fileHandle

然后像这样使用它:

name, handle = openFile()

python 为您处理多个值的返回。

于 2013-08-01T23:27:28.017 回答
0

这是一个返回用户输入的方法

def getInput():
    userInput = raw_input("Enter the name of the key file: ")

    #you can check to see if the input is valid here

    return userInput

这就是您调用该方法的方式

keyFileName = getInput()
于 2013-08-01T23:30:46.687 回答