2014-09-01 79 views
-1

我一直在写一个基于文本的文件系统。为了保存数据,我必须将它写入文件,所以我使用了Pickle。在添加第二个泡菜之前,它似乎工作正常。总之它给我这个错误:腌制Python错误

enter image description here

当年这里是我的代码:

import datetime 
import time 
import pickle 

file_Name = "testfile" 
file_Name2 = "testfile2" 

accounts = {} 
files = {} 

fileObject2 = open(file_Name2,'r') 
accounts = pickle.load(fileObject2) 

fileObject = open(file_Name,'r') 
files = pickle.load(fileObject) 

def startup(): 
    fileObject2 = open(file_Name2,'wb') 
    pickle.dump(accounts, fileObject2) 
    fileObject2.close() 
    print "\n   -------------------   " 
    print "   FILE SYSTEM MANAGER   " 
    print "   -------------------   " 
    print "\n To login type in: LOGIN" 
    print " To create a new account type in: ACCOUNT" 
    loginornew = raw_input("\n Please enter LOGIN or ACCOUNT: ") 
    if loginornew.lower() == "login": 
     login() 
    elif loginornew.lower() == "account": 
     newaccount() 
    else: 
     startup() 

def newaccout(): 
    print "--------------------------------------------" 
    print "\n Would you like to create a new account?" 
    yesorno = raw_input(" Please enter YES or NO: ") 
    if yesorno.lower() == "no": 
     startup() 
    elif yesorno.lower() == "yes": 
     newusername = raw_input("\n Please enter a username for your account: ") 
     newpassword = raw_input(" Please enter a password for your account: ") 
     newpasswordagain = raw_input(" Please confirm the password for your account: ") 
     if newpassword == newpasswordagain: 
      for username in accounts: 
       if username == newusername: 
        print "\n Username already exists" 
        print " Please try again" 
        newaccount() 
       else: 
        pass 
      accounts[newusername] = newpassword 
      print "\n Account Created" 
      startup() 
     else: 
      print "\n Passwords do not match" 
      print " Please try again" 
      newaccount() 
    else: 
     newaccount() 

def login(): 
    print "--------------------------------------------" 
    print "\n Would you like to login?" 
    yesorno = raw_input(" Please enter YES or NO: ") 
    if yesorno.lower() == "no": 
     startup() 
    elif yesorno.lower() == "yes": 
     username = raw_input("\n Please enter your username: ") 
     password = raw_input("\n Please enter your password: ") 
     for usernames in accounts: 
      if username == usernames: 
       for passwords in accounts.keys(): 
        if password == passwords: 
         print "\n Access Granted" 
         print "\n--------------------------------------------" 
         menu() 
        else: 
         login() 
       print "\n Access Denied" 
       print "\n Please try again" 
       print "\n--------------------------------------------" 
       login() 
      elif usernames != usernames: 
       print "\n Access Denied" 
       print "\n Please try again" 
       print "\n--------------------------------------------" 
       login() 
      else: 
       login() 
    else: 
     login() 

def menu(): 
    fileObject = open(file_Name,'wb') 
    pickle.dump(files, fileObject) 
    fileObject.close() 
    print "\n   -------------------   " 
    print "   FILE SYSTEM MANAGER   " 
    print "   -------------------   " 
    print "\n What would you like to do with your files?" 
    print " To make a new file type in: NEW" 
    print " To edit a current file type in: EDIT" 
    print " To delete a current file type in: DELETE" 
    print " To view all current files type in: ALL" 
    chooser = raw_input("\n Please enter NEW, EDIT, DELETE, or ALL: ") 
    if chooser.lower() == "new": 
     newfile() 
    elif chooser.lower() == "edit": 
     editfiles() 
    elif chooser.lower() == "delete": 
     deletefiles() 
    elif chooser.lower() == "all": 
     allfiles() 
    else: 
     menu() 

def newfile(): 
    filename = "" 
    filetext = "" 
    while filename == "": 
     print "--------------------------------------------" 
     filename = raw_input("\n Please input your new files name: ") 
    while filetext == "": 
     filetext = raw_input("\n Please input the text for your new file: ") 
    filedate = datetime.date.today() 
    files[filename] = {filedate:filetext} 
    print "\n File Added" 
    print "\n--------------------------------------------" 
    menu() 

def editfiles(): 
    print "--------------------------------------------" 
    print " To edit a file type in: EDIT" 
    print " To view all current files type in: ALLFILES" 
    print " To cancel type in: CANCEL" 
    wheretogo = raw_input("\n Please enter EDIT, ALLFILES, or CANCEL: ") 
    if wheretogo.lower() == "edit": 
     print "\n To edit a file type in its name" 
     print " To cancel type in: CANCEL" 
     print "\n **Please Note** Editing a file changes its date!" 
     editname = raw_input("\n Please type in the file's name or CANCEL: ") 
     if editname.lower() == "cancel": 
      menu() 
     else: 
      newcontents = "" 
      for filename in files: 
       if filename.lower() == editname.lower(): 
        print "\n What would you like this file to say?" 
        while newcontents == "": 
         newcontents = raw_input("\n Please input files new contents: ") 
        filetext = newcontents 
        filedate = datetime.date.today() 
        files[filename] = {filedate:filetext} 
        print "\n File Changed" 
        print "--------------------------------------------" 
        menu() 
       else: 
        pass 
      print "\n File not found!" 
      editfiles() 
    elif wheretogo.lower() == "allfiles": 
     print "\n--------------------------------------------" 
     for filename in files: 
      print "File Name: " + str(filename) 
     print "--------------------------------------------" 
     print "\n To edit a file type in: EDIT" 
     print " To cancel type in: CANCEL" 
     print "\n **Please Note** Editing a file changes its date!" 
     wheretogo = raw_input("\n Please enter EDIT or CANCEL: ") 
     if wheretogo.lower() == "edit": 
      editname = raw_input("\n Please type in the file's name to edit it: ") 
      newcontents = "" 
      for filename in files: 
       if filename.lower() == editname.lower(): 
        print "\n What would you like this file to say?" 
        while newcontents == "": 
         newcontents = raw_input("\n Please input files new contents: ") 
        filetext = newcontents 
        filedate = datetime.date.today() 
        files[filename] = {filedate:filetext} 
        print "\n File Changed" 
        print "--------------------------------------------" 
        menu() 
       else: 
        pass 
      print "\nFile not found!" 
      editfiles() 
     elif wheretogo.lower() == "cancel": 
      menu() 
     else: 
      menu() 
    elif wheretogo.lower() == "cancel": 
     menu() 
    else: 
     menu() 

def deletefiles(): 
    print "--------------------------------------------" 
    print " To delete a file type in: DELETE" 
    print " To view all current files type in: ALLFILES" 
    print " To cancel type in: CANCEL" 
    wheretogo = raw_input("\n Please enter DELETE, ALLFILES, or CANCEL: ") 
    if wheretogo.lower() == "delete": 
     print "\n To delete a file type in its name" 
     print " To cancel type in: CANCEL" 
     deletename = raw_input("\n Please type in the file's name or CANCEL: ") 
     if deletename.lower() == "cancel": 
      menu() 
     else: 
      for filename in files: 
       if filename.lower() == deletename.lower(): 
        del files[filename] 
        print "\n File Removed" 
        print "--------------------------------------------" 
        menu() 
       else: 
        pass 
      print "\n File not found!" 
      deletefiles() 
    elif wheretogo.lower() == "allfiles": 
     print "\n--------------------------------------------" 
     for filename in files: 
      print "File Name: " + str(filename) 
     print "--------------------------------------------" 
     print "\n To delete a file type in: DELETE" 
     print " To cancel type in: CANCEL" 
     wheretogo = raw_input("\n Please enter DELETE or CANCEL: ") 
     if wheretogo.lower() == "delete": 
      deletename = raw_input("\n Please type in the file's name to delete it: ") 
      for filename in files: 
       if filename.lower() == deletename.lower(): 
        del files[filename] 
        print "\n File Removed" 
        print "--------------------------------------------" 
        menu() 
       else: 
        pass 
      print "\nFile not found!" 
      deletefiles() 
     elif wheretogo.lower() == "cancel": 
      menu() 
     else: 
      menu() 
    elif wheretogo.lower() == "cancel": 
     menu() 
    else: 
     menu() 

def allfiles(): 
    filetexttotal = "" 
    for filename in files: 
     print "\n--------------------------------------------" 
     print "\nFile Name: " + str(filename) 
     for filedate in files[filename]: 
      print "File Date: " + str(filedate) 
      for filetext in files[filename][filedate]: 
       filetexttotal = filetexttotal + str(filetext) 
      print "File Contents: " + str(filetexttotal) 
      filetexttotal = "" 
    print "\n--------------------------------------------" 
    menu() 

login() 
+1

您是否熟悉[Short,Self Contained,Correct(Compilable),Example](http://sscce.org/)的概念?它可能会帮助你在自己的代码中找到自己的错误,如果你仍然无法解决它,那么它可以帮助我们帮助你。 – 2014-09-01 02:02:23

+1

您在加载后没有关闭文件句柄。坏的东西随之而来。这是您应该始终使用上下文管理器打开(并隐式地关闭()')文件的许多原因之一。 – roippi 2014-09-01 02:15:40

回答

1

尝试打开文件与 'RB' 模式。像这样:

... 
fileObject2 = open(file_Name2,'rb') 
... 

告诉我,如果它的工作。

+0

他倾倒协议0,这是基于ASCII的协议,因此在'r'中打开实际上是可行的。实际上最好的做法是打开'rb',因为它涵盖了所有的平台和协议,但它不会解决OP的缺陷。 – roippi 2014-09-01 03:45:51

+0

你好,我试图用'rb'打开文件,但它仍然不起作用。 – 2014-09-01 14:44:22

+0

@ user3600560还是一样的错误? – 2014-09-01 22:30:41