Python 如果用户写错了,请要求用户写正确的文件名

Python 如果用户写错了,请要求用户写正确的文件名,python,Python,我想使用下面的上传功能上传一些文件 我要求用户写下他想要上传的文件名 但我之前尝试过一些验证,如果用户写入的文件名不存在,我想显示一条消息您选择的文件不存在,请重试,我想再次要求用户写入相同的文件名,以便他能够正确写入。你知道我怎样才能做到这一点吗 def upload(filename): if(os.path.exists(filename)): # here I do the upload else: print "The file you

我想使用下面的上传功能上传一些文件

我要求用户写下他想要上传的文件名

但我之前尝试过一些验证,如果用户写入的文件名不存在,我想显示一条消息您选择的文件不存在,请重试,我想再次要求用户写入相同的文件名,以便他能够正确写入。你知道我怎样才能做到这一点吗

def upload(filename):
    if(os.path.exists(filename)):
        # here I do the upload
    else:
        print "The file you selected does not exist, please try again"
        # here I want to ask user again for the field that he filled wrong
        # but this needs to be dynamic (first, second or third)
        uploadToS3(input("Select the first/second/third file:"))
print

raw_input("Press enter to transfer the files...")

upload(input("Select the first file:"))
upload(input("Select second file:"))
upload(input("Select third file:"))
由于您一步一个文件地上传文件,因此可以通过将它们锁定在循环中,在函数中一次验证一个文件。成功上载后返回循环外并运行:

def upload(filename):
    while True:
        if(os.path.exists(filename)):
            # here I do the upload
            return
        else:
            print "The file you selected does not exist, please try again"
            filename = input("Select the first/second/third file:")

像这样的怎么样?这是一个快速的黑客,但可能会奏效

def upload(filename):
    if(os.path.exists(filename)):
        # here I do the upload
        return True
    else:
        print "The file you selected does not exist, please try again"
        # here I want to ask user again for the field that he filled wrong
        # but this needs to be dynamic (first, second or third)
        uploadToS3(input("Select the first/second/third file:"))
print
return False

def read_user_input(prompt):
    oper_status = upload(input(prompt))
    while oper_status is not True:
        oper_status = upload(input(prompt))


raw_input("Press enter to transfer the files...")

read_user_input(input("Select the fist file:"))
read_user_input(input("Select second file:"))
read_user_input((input("Select third file:"))

我可能会这样做:

def get_filename(prompt):
    while True:
        fn = raw_input(prompt)
        if os.path.exists(fn): return fn
        print("The file you selected does not exist, please try again")


uploadToS3(get_filename("Select the fist file:"))
uploadToS3(get_filename("Select second file:"))
uploadToS3(get_filename("Select third file:"))
这也摆脱了你的上传功能

这种方法定义了一个名为get_filename的函数,该函数接受要显示的提示作为参数

将显示提示并收集输入

如果输入文件名存在,则返回uploadToS3


如果输入文件名不存在,则会打印一条消息,并提示重新发出、重新收集输入等。

尝试一个简单的while循环。当输入的文件名不存在时,while循环将保持为True。一旦用户输入一个存在的文件名,它将中断while循环并继续上传部分。当然,您应该调整编程逻辑以满足您的需要

def upload(filename):
    while True:
        if not(os.path.exists(filename)):
            print "The file you selected does not exist, please try again"
        else:
            #upload stuff
            break

我使用递归的答案是:

def upload(prompt):
    filename = input(prompt) #ask for filename using the given prompt
    if(os.path.exists(filename)):
        uploadToS3(filename)
    else:
        print "The file you selected does not exist, please try again"
        upload(prompt) #Repeat this function if the use did not give valid input
print

raw_input("Press enter to transfer the files...")

upload("Select the fist file:")
upload("Select second file:")
upload("Select third file:")

第二个输入将显示与第一个输入完全相同的消息。

这是什么意思,但需要是动态的第一、第二或第三个输入?你能详细说明一下吗?你的意思是,如果第一个输入要求第二个文件,那么函数生成的第二个输入应该要求第二个文件,依此类推?你的代码将不起作用,因为你使用的是原始输入和输入。一般来说,如果答案中包含对代码用途的解释,那么答案会更有帮助,为什么不引入其他人就解决了问题。