【发布时间】:2015-03-19 19:59:59
【问题描述】:
我正在制作一个将文件内容加密为密文的程序。我的问题是,当我的程序要求用户输入他们想要加载的文件的名称并且用户没有给出有效的响应时,就会出现“FileNotFoundError:”。我希望我的程序有一个功能,如果用户没有给出有效的响应,程序会不断告诉用户重试。
def EncryptCode():
encryptFileLoad = input("Name the file and directory you want to load with the ending '.txt':\n")
with open (encryptFileLoad,mode="r",encoding="utf=8") as encrypt_file:
encryptFile = encrypt_file.read()
我收到这样的错误:
Traceback (most recent call last):
File "C:\...
FileNotFoundError: [Errno 2] No such file or directory: 'test.txt'
我尝试过这样做:
def EncryptCode():
...
try:
...
except FileNotFoundError:
return EncryptCode
【问题讨论】:
标签: python filenotfoundexception file-not-found