首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Python程序无法打开txt文件

Python程序无法打开txt文件
EN

Stack Overflow用户
提问于 2016-04-09 05:47:34
回答 1查看 1.2K关注 0票数 0

我是python的新手,我想做一个密码管理器。问题是,当我通过IDLE或Pycharm运行我的程序时,它运行得很好,但当我直接从windows运行它时,当它到达我导入存储密码的文件的那一行时,它就会停止运行。

代码语言:javascript
复制
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)

我尝试删除导入文本文件的行,但它起作用了。我不知道为什么代码在从窗口打开时无法打开文件,而在从IDLE或Pycharm打开时却可以打开它

EN

回答 1

Stack Overflow用户

发布于 2016-04-09 07:08:06

“当我从windows运行它时,它就会崩溃(右键单击并使用==> python.exe打开它)”

执行此操作时,工作目录为C:\Windows\system32。很可能您对此目录没有写入权限。当使用其他方法运行脚本时,工作目录很可能是包含脚本的目录。您需要切换到您有写入权限的目录。以下是使用当前用户的Documents文件夹的示例:

import os dir = os.path.expanduser('~/Documents') os.chdir(dir)

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36509855

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档