灵异事件,关于coercing to Unicode: need string or buffer, NoneType found

加盾男爵 2016-09-24 05:09:20
写了一段代码,是一个累,需要读写本地(当前目录)的文件
其中有个要求,在累的删除被删除的时候需要在本地文件中记录时间(最后登陆时间)

然后在__del__(self)里有类似代码

try:
Fi = open("FileName","r")
except Exception e:
print e
else:
print "Opne Susseccful"

运行结果
coercing to Unicode: need string or buffer, NoneType found
当然这段代码是位于__del__(self)函数内的,我把这段函数单独写上一个脚本运行,结果输出
Opne Susseccful
我想问下,为什么2行打开文件的代码(两个open)函数,代码复制出来就没问题,但放在函数里运行就有问题?

~~~~~~~我是华丽的分割线~~~~~~~~~~
这里是我写的代码
72-73这两行抛出异常coercing to Unicode: need string or buffer, NoneType found
#!/usr/bin/env python
import os # for os.linesep
import time #for last time
AccountFile = "./account.txt"
PassWDFile = "./passWD.txt"
LastRunTimeFile = "./LastTime.txt"
IOSTIME = "%Y-%m-%d %X" #Show time formart
class SQLCoustomer(object):

def CheckAccount(self):
CheckResult = False
try:
AllAccount = open(AccountFile,"r")
AllPassWD = open(PassWDFile,"r")
except Exception:
print "Open Wrong._1"
else:
dbA = AllAccount.readlines()
dbP = AllPassWD.readlines()
AllAccount.close() #close local file
AllPassWD.close()
AS = self.Account + os.linesep
if not dbA: #empty file ,and this is run first
pass
if AS not in dbA: #Test the account is exist or not
pass
else:
i = 0
PD = self.PassWD + os.linesep
for ac in dbA: #tset the passWDis right or not
if AS == ac and dbP[i] == PD:
CheckResult = True
break
i += 1
self.Login = CheckResult
if self.Login == True: #it's exist account and right password ,now we can get run time
try:
TFile = open(LastRunTimeFile,"r")
AllAccount = open(AccountFile,"r")
except Exception :
print "Open Time File Error_2"
else:
dbA = AllAccount.readlines()
dbT = TFile.readlines()
AllAccount.close()
TFile.close()
ThisIndex = dbA.index(self.Account + os.linesep)
self.LastTime = ""
for ch in dbT[ThisIndex]:
if ch != os.linesep:
self.LastTime += ch
return CheckResult

def __init__(self,Account,PassWD):
self.Account = Account
self.PassWD = PassWD
self.Login = False
if not os.path.exists(AccountFile): #create thrid files when th programme run first time
F = open(AccountFile,"a")
F.close()
if not os.path.exists(PassWDFile):
F = open(PassWDFile,"a")
F.close()
if not os.path.exists(LastRunTimeFile):
F = open(LastRunTimeFile,"a")
F.close()


def __del__(self):
if self.Login:
try:
AllRunTimeFile = open(LastRunTimeFile,"r")
AllAccount = open(AccountFile,"r")
except Exception,e:
print e
else:
dbA = AllAccount.readlines()
dbT = AllRunTimeFile.readlines()
ThisAccount = self.Account + os.linesep
ThisIndex = 0
for ch in dbA:
if ch == ThisAccount:
dbT[ThisIndex] = time.strftime(IOSTIME,time.localtime())
AllRunTimeFile.writelines(dbT)
F = open(LastRunTimeFile,"w")
F.write.lines(dbT)
F.close()
break
ThisIndex += 1
AllAcount.close()
AllRunTimeFile.close()

def ShowLoginInfo(self): #show login is successful or not
self.CheckAccount()
if self.Login == True:
print "Login Successful"
else:
print "Account Isn't Exist Or Wrong Password."
return self.Login

def ShowCoustomInfo(self): #show the infomation of coustomer when he/she is login successfull
if self.Login == False:
print "You Hadn't Logined Successful."
return
print "Account Is %s"%self.Account
print "You Password Is %s"%self.PassWD
print "You Last Time Of Login Is %s"%self.LastTime

def CreatAccount(self):
try:
AF = open(AccountFile,"r")
db = AF.readlines()
AF.close()
except Exception:
print "File Open Fail_4"
else:
SA = self.Account + os.linesep
if SA in db:
print "%s Had Existed,Can't Creat."%self.Account
return
try:
NewAllAccount = open(AccountFile,"a")
NewAllPassWD = open(PassWDFile,"a")
NewRunTimeFile = open(LastRunTimeFile,"a")
except Exception:
print "File Error._5"
else:
WriteAccount = self.Account + os.linesep
NewAllAccount.writelines(WriteAccount)
WritePassWD = self.PassWD + os.linesep
NewAllPassWD.writelines(WritePassWD)
WriteLastTime = time.strftime(IOSTIME,time.localtime()) + os.linesep
NewRunTimeFile.writelines(WriteLastTime)
print "%s Has Created."%self.Account
NewAllAccount.close()
NewAllPassWD.close()
NewRunTimeFile.close()

if __name__ == "__main__":
print"A:Login B:Creat An New Account"
YourChoice = raw_input()
if YourChoice == "A":
print "Please Input Your Account:",
Account_1 = raw_input()
print "Please Input Your PassWord:",
PassWD_1 = raw_input()
ExistAccount_1 = SQLCoustomer(Account_1,PassWD_1)
if ExistAccount_1.ShowLoginInfo():
ExistAccount_1.ShowCoustomInfo()
elif YourChoice == "B":
print "Please Input Your Account:",
Account_2 = raw_input()
print "Please Input Your PassWord:",
PassWD_2 = raw_input()
NewAccount_1 = SQLCoustomer(Account_2,PassWD_2)
NewAccount_1.CreatAccount()
else:
print "Done!"
print "Bye!"


然后我把这两端代码复制出来做出一个单独的脚本,下面这个脚本里的代码除了最后以后一行以外,都是在vim里面有yy和p命令复制过来的
#!/usr/bin/env python
AccountFile = "./account.txt"
LastRunTimeFile = "./LastTime.txt"
try:
AllAccount = open(AccountFile,"r")
AllRunTimeFile = open(LastRunTimeFile,"r")
except Exception,e:
print e
else:
print "ok"

但这个脚本运行的时候输出结果是
ok
并没有抛出异常,

我想问下这个是神马情况
...全文
607 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
liwwwe 2016-09-27
  • 打赏
  • 举报
回复
1。你在程序末尾手动del existAccount_1。 2。或者,那些代码不要写在__main__中,写个函数,然后在main中调用函数。 3。你在del中open之前打印那两个文件名变量看看,相信会有所收获。其实open出错后,程序就被终止了,else里面没执行
加盾男爵 2016-09-26
  • 打赏
  • 举报
回复
引用 5 楼 weditor 的回复:
原谅我上面的回复。刚看了代码,正式的代码并没有错。 del并不是你手动调用,而是系统调用, 现场存在很多全局变量,ExistAccount_1 是 SQLCoustomer的全局变量,因为存在与__main__中, 所以ExistAccount_1, AccountFile, PassWDFile,LastRunTimeFile的销毁顺序是不确定的, 如果那个类最后销毁,就会导致open(LastRunTimeFile,"r")这种代码变成open(None, 'r')
关键问题是这个程序能运行,我预期的功能都能实现。。。。
liwwwe 2016-09-26
  • 打赏
  • 举报
回复
原谅我上面的回复。刚看了代码,正式的代码并没有错。 del并不是你手动调用,而是系统调用, 现场存在很多全局变量,ExistAccount_1 是 SQLCoustomer的全局变量,因为存在与__main__中, 所以ExistAccount_1, AccountFile, PassWDFile,LastRunTimeFile的销毁顺序是不确定的, 如果那个类最后销毁,就会导致open(LastRunTimeFile,"r")这种代码变成open(None, 'r')
liwwwe 2016-09-26
  • 打赏
  • 举报
回复
看到第一段代码,
except Exception e:
    print e
python2应该是这样,逗号隔开:
except Exception,e:
    print e
python3是: except Exception as e: print e 没见过用空格的。或许是我孤陋寡闻了。囧。
panghuhu250 2016-09-26
  • 打赏
  • 举报
回复
把所有的错误信息都贴出来。
加盾男爵 2016-09-25
  • 打赏
  • 举报
回复
引用 1 楼 sprawling 的回复:
72,87的代码再仔细看看,处理的有点问题, 比如写的没有用w打开, 另外文件又再没有关闭的情况下二次打开也会又问题把。
87行代码的close()对应的是85行的open()刚开始的时候我也觉得是打开了一个未打开的文件,后来我仔细排查了下,应该不是,而且就算是,怕抛出的异常也不该是coercing to Unicode: need string or buffer, NoneType found 最主要的是抛出异常的是try块里的那2段代码,我实在想不出,为什么打开一个文件会抛出这个异常
sprawling 2016-09-24
  • 打赏
  • 举报
回复
72,87的代码再仔细看看,处理的有点问题, 比如写的没有用w打开, 另外文件又再没有关闭的情况下二次打开也会又问题把。

37,720

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧