如果用户写错了,请让用户写出正确的文件名

时间:2015-03-28 00:24:46

标签: python

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

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

但是我之前尝试进行某种验证,如果用户写了一个不存在的文件名我想要显示一条消息"你选择的文件不存在,请再试一次"我想再次要求用户写相同的文件名,以便他能正确写入。你知道我怎么能实现这个目标?

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:"))

5 个答案:

答案 0 :(得分:3)

由于您逐个文件逐步执行upload(),您可以通过将它们锁定在循环中来一次验证它们。成功上传后,return退出循环(和函数):

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:")

答案 1 :(得分:2)

我可能会做以下事情:

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:"))

这也摆脱了你的upload功能。

这种方法定义了一个名为get_filename的函数,它接受您想要呈现的提示作为参数。

显示提示并收集输入。

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

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

答案 2 :(得分:1)

这样的事情怎么样?这是一个快速的黑客,但可能会有效。

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:"))

答案 3 :(得分:1)

尝试一个简单的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

答案 4 :(得分:1)

我使用递归的答案是:

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:")

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