37,720
社区成员
发帖
与我相关
我的任务
分享
try:
Fi = open("FileName","r")
except Exception e:
print e
else:
print "Opne Susseccful"
#!/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!"
#!/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"
except Exception e:
print e
python2应该是这样,逗号隔开:
except Exception,e:
print e
python3是:
except Exception as e:
print e
没见过用空格的。或许是我孤陋寡闻了。囧。