Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/310.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用户输入的Python FileNotFoundError_Python_Filenotfoundexception_File Not Found - Fatal编程技术网

用户输入的Python FileNotFoundError

用户输入的Python FileNotFoundError,python,filenotfoundexception,file-not-found,Python,Filenotfoundexception,File Not Found,我正在制作一个程序,将文件内容加密为密文。我的问题是,当我的程序要求用户输入他们想要加载的文件名,而用户没有给出有效的响应时,就会出现“FileNotFoundError:”。我希望我的程序有一个功能,如果用户没有给出有效的响应,程序将继续告诉用户重试 def EncryptCode(): encryptFileLoad = input("Name the file and directory you want to load with the ending '.txt':\n")

我正在制作一个程序,将文件内容加密为密文。我的问题是,当我的程序要求用户输入他们想要加载的文件名,而用户没有给出有效的响应时,就会出现“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
我试过这样做:

Traceback (most recent call last):
    File "C:\...
FileNotFoundError: [Errno 2] No such file or directory: 'test.txt'
def EncryptCode():
    ...
    try:
        ...
    except FileNotFoundError:
        return EncryptCode
那怎么办

def EncryptCode():
    file_not_found = True
    while(file_not_found):
        try:
            encryptFileLoad = input("Name the file and directory you want to load with the ending '.txt':\n")
            file_not_found = False
        except FileNotFoundError:
            print('that didnt work! try again')
    with open (encryptFileLoad,mode="r",encoding="utf=8") as encrypt_file:
        encryptFile = encrypt_file.read()
你几乎成功了。检查它应该是这样的:

def EncryptCode():
    try:
        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()
            return encryptFile
    except FileNotFoundError:
        print('File not found. Input correct filename')
        return EncryptCode()
或者您可以使用
while
循环要求用户输入正确的文件名,如:

while True:
    try:
        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()
        break
    except FileNotFoundError:
        print('File not found. Input correct filename')