|
初学Python,写了个很菜的程序,实在不知道哪儿出毛病了,请高手看看,谢谢!
#Filename:mab_ver1.py
import cPickle as cp
import sys
mylist='myinfolist.data'
maindict={}
mainlist=[]
class Person:
def __init__(self,n,a,p):
self.name=n
self.addr=a
self.phon=p
def writeData(self):
f=file(mylist,'w')
f.close()
f=file(mylist)
mainlist=cp.load(f)
for k,v in mainlist:
maindict[k]=v
maindict[self.name]="%s %s"%(self.addr,self.phon)
mainlist=maindict.items()
f=file(mylist,'w')
cp.dump(mainlist,f)
f.close()
if len(sys.argv)<2:
print 'No acion specified.'
sys.exit()
if sys.argv[1].startswith('add'):
name=raw_input('Please Enter the Name:')
addr=raw_input('Please Enter the Address:')
phon=raw_input('Please Enter the Phone Number:')
p=Person(name,addr,phon)
p.writeData()
sys.exit()
if sys.argv[1].startswith('del'):
name=raw_input('Please Enter the Name You Want to Delete:')
f=file(mylist,'w')
mainlist=cp.load(f)
for k,v in mainlist:
maindict[k]=v
if name in mainlist:
del maindict[name]
else:
print "%s doesn't exist" %name
mainlist=maindict.items()
cp.dump(mainlist,f)
f.close()
sys.exit()
每次运行时都会报错:
Traceback (most recent call last):
File "C:\Python23\Lib\site-packages\Pythonwin\pywin\framework\scriptutils.py", line 310, in RunScript
exec codeObject in __main__.__dict__
File "C:\Documents and Settings\zchar\My Documents\mab_ver1.py", line 37, in ?
p=Person(name,addr,phon)
File "C:\Documents and Settings\zchar\My Documents\mab_ver1.py", line 19, in writeData
mainlist=cp.load(f)
EOFError |
|