2016-04-08 20 views
-1

私はPythonには新しく、パスワードマネージャを作成しようとしています。 問題は、IDLEまたはPycharm でプログラムを実行したときにプログラムがうまく動作するが、パスワードを保存するファイルをインポートする行に到達したときにウィンドウから直接実行すると実行が停止するという問題がある。Pythonプログラムでtxtファイルを開くことができません

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から開いたときに開くことができます

+0

このコードのすべては、次のとおりです。 ここでは現在のユーザーのドキュメントフォルダを使用する例がありますか?エラーメッセージが表示されますか?それとも、ファイルに書き込まれませんか? – kirkpatt

+0

どのようなエラーが出ますか。 IDLEについては特に分かりませんが、IDEを使用するときは現在の作業ディレクトリを別のものとすることができます。 –

+0

はいこれはすべてのコードであり、IDLEとPycharmの両方でうまく動作します。それはちょうど私がWindowsから(rightclick ==> python.exeで開く==>)エラーメッセージを表示するが、プログラムがエラーメッセージの後にimmediatlyの実行を停止するので、私が言うことを読むことができません。 – PrOoOg

答えて

0

"Windowsから実行するとちょっとクラッシュします。 ==> python.exe) "

これを行うと、作業ディレクトリはC:\ Windows \ system32になります。ほとんどの場合、このディレクトリへの書き込み権限はありません。他の方法を使用してスクリプトを実行する場合、作業ディレクトリはおそらくスクリプトを含むものです。あなたは書き込み権限を持つディレクトリに変更する必要があります。

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

関連する問題