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

Fav*_*las 0 python

我正在使用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')
Run Code Online (Sandbox Code Playgroud)

如何将其转换为循环,以便如果用户输入错误的文件名或文件不在该位置,它会继续询问该文件?

问候,

Favolas

Sve*_*ach 7

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