Python 2.7 Python程序可以';打开txt文件
我是python新手,我正在尝试制作一个密码管理器。 问题是,当我在空闲或空闲状态下运行程序时,它工作正常 但当我直接从windows运行它时,当它到达我导入文件并存储密码的行时,它就会停止运行Python 2.7 Python程序可以';打开txt文件,python-2.7,Python 2.7,我是python新手,我正在尝试制作一个密码管理器。 问题是,当我在空闲或空闲状态下运行程序时,它工作正常 但当我直接从windows运行它时,当它到达我导入文件并存储密码的行时,它就会停止运行 import time user = raw_input("Username: ") pw = raw_input("Password: ") if user == "PrOoOg" and pw == "aka443": def add_acc_func(acc, user, pw):
import time
user = raw_input("Username: ")
pw = raw_input("Password: ")
if user == "PrOoOg" and pw == "aka443":
def add_acc_func(acc, user, pw):
database.write("\nAccount: ")
database.write(acc)
database.write(" Username: ")
database.write(user)
database.write(" Password: ")
database.write(pw)
database.close()
def print_database_func():
for lines in database.readlines():
print lines.strip('\n')
database.close()
user_input = raw_input('Press "A" to add a new account\nPress "M" to modify and existing account\n'
'Press "D" to delete and existing account\nPress "S" to show all accounts and passwords\n')
user_choice = user_input.lower()
if user_choice == "a":
database = open("source.txt", "a") #Everything worked fine when i deleted this line
acc_to_add = raw_input("Write the name of the site or service: ").lower()
acc_to_add_user = raw_input("Write the username or email you want to set for that account: ")
acc_to_add_pw = raw_input("Write the password you want to set to that account: ")
add_acc_func(acc_to_add, acc_to_add_user, acc_to_add_pw)
print "Account added"
if user_choice == "s":
database = open("source.txt", "r") #Everything worked fine when i deleted this line
print_database_func()
raw_input("Press Enter to quit")
else:
print ("Wrong username or password")
time.sleep(3)
我试图删除导入文本文件的行,结果成功了。
我不知道为什么代码在从windows打开时无法打开文件,在从IDLE或Pycharm打开时也可以打开文件“当我从windows运行它时,它只是崩溃了(右击==>用==>python.exe打开)”
执行此操作时,工作目录为C:\Windows\system32。很可能您没有对此目录的写入权限。使用其他方法运行脚本时,工作目录很可能是包含脚本的目录。您需要更改到具有写入权限的目录。
以下是使用当前用户的文档文件夹的示例:
导入操作系统
dir=os.path.expanduser(“~/Documents”)
os.chdir(dir)
这就是全部代码吗?您是否收到任何错误消息,或者它只是不写入文件?它会给出什么错误。我不知道IDLE的具体情况,但这可能与使用IDE时当前工作目录不同有关。当我从windows运行它时(右键单击==>open with==>python.exe),它会打印一条错误消息,但我无法读取它所说的内容,因为程序在错误消息出现后立即停止运行。从命令行运行它如何从命令行运行它?