繁体   English   中英

如果条件未经验证,Python将创建一个循环

[英]Python create a loop if condition not verified

我正在使用python 2.6.6,但无法解决问题。

我有以下代码:

file = raw_input('Enter the name of the file: ')
try:
    text_file = open(file,'r')
except IOError:
    print 'File not found'
    file = raw_input('Enter the name of the file: ')
    text_file = open(file,'r')

我如何将其变成一个循环,以便如果用户输入了错误的文件名或文件名不在该位置,它将继续请求文件?

问候,

法沃拉斯

while True:
    file = raw_input('Enter the name of the file: ')
    try:
        text_file = open(file,'r')
        break
    except IOError:
        print 'File not found'

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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