Python中的“无此类文件或目录”

时间:2018-09-19 13:21:39

标签: python

我正在尝试创建一个加密程序,该程序还可以使用用户名和密码进行访问,并且可以更改密码,但是,尝试从中读取密码时出现以下错误一个文件。

Traceback (most recent call last):
  File "C:/Users/Matthew/AppData/Local/Programs/Python/Python37-32/a.py", line 28, in <module>
    password()
  File "C:/Users/Matthew/AppData/Local/Programs/Python/Python37-32/a.py", line 9, in password
    var2 = open("Users\Matthew\AppData\Local\Programs\Python\Python37-32\password.txt","r")
FileNotFoundError: [Errno 2] No such file or directory: 'Users\\Matthew\\AppData\\Local\\Programs\\Python\\Python37-32\\password.txt'

密码保存在Users\Matthew\AppData\Local\Programs\Python\Python37-32\password.txt directory中。

下面是代码。

import os
import time
def password():
    while True:
        username = input ("Enter Username: ")
        password = input ("Enter Password: ")

        var1 = "admin"
        var2 = open("Users\Matthew\AppData\Local\Programs\Python\Python37-32\password.txt","r")
        if username == var1 and password == var2:
            time.sleep(1)
            print ("Login successful!")
            answer = input("Do you wish to change your password (Y/N): ")
            if input == "Y" or "y":
                var2 = input("Enter new password: ")
            elif input == "N" or "n":
                break
            logged()
            break

        else:
            print ("Password did not match!")

def logged():
    time.sleep(1)
    print ("Welcome to the encryption program.")

password()

def main():
    result = 'Your message is: '
    message = ''
    choice = 'none'

    while choice != '-1':
        choice = input("\nDo you want to encrypt or decrypt the message?\nEnter 1 to Encrypt, 2 to Decrypt, -1 to Exit Program: ")

        if choice == '1':
            message = input("\nEnter the message to encrypt: ")

            for i in range(0, len(message)):
                result = result + chr(ord(message[i]) - 2)

                print (result + '\n\n')
                result = ''

        elif choice == '2':
            message = input("\nEnter the message to decrypt: ")

            for i in range(0, len(message)):
                result = result + chr(ord(message[i]) + 2)

                print (result + '\n\n')
                result = ''

        elif choice != '-1':
            print ("You have entered an invalid choice. Please try again.\n\n")

        elif choice == '-1':
            exit()

main()

感谢您的帮助,谢谢!

1 个答案:

答案 0 :(得分:0)

提供完整路径:

var2 = open("C:/Users/Matthew/AppData/Local/Programs/Python/Python37-32/password.txt","r")

编辑:

正如您在评论中说的那样,它可以工作,但是密码被标记为不正确,因此我已解决了您的代码问题。 您无法通过打开文件直接读取数据。您将必须使用命令read来获取数据:

file = open("C:/Users/Matthew/AppData/Local/Programs/Python/Python36/password.txt","r")
var2 = file.read()
file.close()

您的第二个代码问题是设置新密码。您编写的代码:

answer = input("Do you wish to change your password (Y/N): ")
if input == "Y" or "y":
    var2 = input("Enter new password: ")
elif input == "N" or "n":
    break

不要使用input来查看值,而要使用存储输入数据的变量。另外,lower字符串也很容易:

answer = input("Do you wish to change your password (Y/N): ")
if answer.lower() == "y":
    var2 = input("Enter new password: ")
elif answer.lower() == "n":
    break

完整代码如下:

import os
import time
def password():
    while True:
        username = input ("Enter Username: ")
        password = input ("Enter Password: ")

        var1 = "admin"
        file = open("C:/Users/Matthew/AppData/Local/Programs/Python/Python36/password.txt","r")
        var2 = file.read()
        file.close()
        if username == var1 and password == var2:
            time.sleep(1)
            print ("Login successful!")
            answer = input("Do you wish to change your password (Y/N): ")
            if answer.lower() == "y":
                var2 = input("Enter new password: ")
            elif answer.lower() == "n":
                break
            logged()
            break

        else:
            print ("Incorrect Information!")

def logged():
    time.sleep(1)
    print ("Welcome to the Encryption program.")

password()

def main():
    result = 'Your message is: '
    message = ''
    choice = 'none'

    while choice != '-1':
        choice = input("\nDo you want to encrypt or decrypt the message?\nEnter 1 to Encrypt, 2 to Decrypt, -1 to Exit Program: ")

        if choice == '1':
            message = input("\nEnter the message to encrypt: ")

            for i in range(0, len(message)):
                result = result + chr(ord(message[i]) - 2)

                print (result + '\n\n')
                result = ''

        elif choice == '2':
            message = input("\nEnter the message to decrypt: ")

            for i in range(0, len(message)):
                result = result + chr(ord(message[i]) + 2)

                print (result + '\n\n')
                result = ''

        elif choice != '-1':
            print ("You have entered an invalid choice. Please try again.\n\n")

        elif choice == '-1':
            exit()

main()