First of all, the important parts of the code... - def AddressRW(aw, ar):
-
#For Reading & Writing to address file
-
while aw == True:
-
try:
-
#(W)right
-
adDict = {}
-
adFile = file('address.txt', 'r')
-
adDict = Dill.load(adFile)
-
adFile = file('address.txt', 'w')
-
newName = raw_input("Enter name of resident: ")
-
if adDict.has_key(newName):
-
adFile.close
-
ynTest = True
-
while ynTest == True:
-
overChoice = raw_input("Name already in list. Rewright? (y,n)")
-
if overChoice == 'y' or overChoice == 'Y':
-
aw = False
-
ynTest = False
-
AddressAP(True, False, False)
-
elif overChoice == 'n' or overChoice == 'N':
-
aw = False
-
ynTest = False
-
else:
-
print "...Error - Try Again..."
-
else:
-
adDict[newName] = raw_input('Enter Address in desired format: ')
and... - def AddressAP(edit, delete, search):
-
#For searching, editing, and deleting address names
-
while edit == True:
-
try:
-
adDict = {}
-
adFile = file('address.txt', 'r')
-
adDict = Dill.load(adFile)
-
adFile = file('address.txt', 'w')
-
dictSearch = raw_input("Whose address do you want to edit? ")
-
if adDict.has_key(dictSearch):
-
adDict[dictSearch] = raw_input("What is the new address? ")
-
Dill.dump(adDict, adFile)
-
adFile.close
-
edit = False
-
else:
-
print "Name not on file."
-
edit = False
-
except EOFError:
-
print "No addresses on file..."
-
edit = False
-
When you run the code and run into a name that's already on file, you get the option to go onto the edit section of the code. When you do this, though, the program always gets the EOFError. I'm guessing the data in the file is deleted (or moved directly to? excuse my ignorance) when it's moved into memory. It's not put back until you leave both functions. I'm not sure how to get around this.
The other problem is that on my school computer, it doesn't recognize 'address.txt' as being there. I keep getting this message: - Traceback (most recent call last):
-
File "C:\Python24\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 310, in RunScript
-
exec codeObject in __main__.__dict__
-
File "C:\Documents and Settings\student4\Desktop\Python Programs\Addressbook\addressbook.py.py", line 140, in ?
-
AddressRW(False, True)
-
File "C:\Documents and Settings\student4\Desktop\Python Programs\Addressbook\addressbook.py.py", line 53, in AddressRW
-
adFile = file('address.txt', 'r')
-
IOError: [Errno 2] No such file or directory: 'address.txt'
-
But I think this probably has something to do with the way the cmd prompt is setup...
3 1708 bvdet 2,851
Expert Mod 2GB
First of all, the important parts of the code... - def AddressRW(aw, ar):
-
#For Reading & Writing to address file
-
while aw == True:
-
try:
-
#(W)right
-
adDict = {}
-
adFile = file('address.txt', 'r')
-
adDict = Dill.load(adFile)
-
adFile = file('address.txt', 'w')
-
newName = raw_input("Enter name of resident: ")
-
if adDict.has_key(newName):
-
adFile.close
-
ynTest = True
-
while ynTest == True:
-
overChoice = raw_input("Name already in list. Rewright? (y,n)")
-
if overChoice == 'y' or overChoice == 'Y':
-
aw = False
-
ynTest = False
-
AddressAP(True, False, False)
-
elif overChoice == 'n' or overChoice == 'N':
-
aw = False
-
ynTest = False
-
else:
-
print "...Error - Try Again..."
-
else:
-
adDict[newName] = raw_input('Enter Address in desired format: ')
and... - def AddressAP(edit, delete, search):
-
#For searching, editing, and deleting address names
-
while edit == True:
-
try:
-
adDict = {}
-
adFile = file('address.txt', 'r')
-
adDict = Dill.load(adFile)
-
adFile = file('address.txt', 'w')
-
dictSearch = raw_input("Whose address do you want to edit? ")
-
if adDict.has_key(dictSearch):
-
adDict[dictSearch] = raw_input("What is the new address? ")
-
Dill.dump(adDict, adFile)
-
adFile.close
-
edit = False
-
else:
-
print "Name not on file."
-
edit = False
-
except EOFError:
-
print "No addresses on file..."
-
edit = False
-
When you run the code and run into a name that's already on file, you get the option to go onto the edit section of the code. When you do this, though, the program always gets the EOFError. I'm guessing the data in the file is deleted (or moved directly to? excuse my ignorance) when it's moved into memory. It's not put back until you leave both functions. I'm not sure how to get around this.
The other problem is that on my school computer, it doesn't recognize 'address.txt' as being there. I keep getting this message: - Traceback (most recent call last):
-
File "C:\Python24\Lib\site-packages\pythonwin\pywin\framework\scriptutils.py", line 310, in RunScript
-
exec codeObject in __main__.__dict__
-
File "C:\Documents and Settings\student4\Desktop\Python Programs\Addressbook\addressbook.py.py", line 140, in ?
-
AddressRW(False, True)
-
File "C:\Documents and Settings\student4\Desktop\Python Programs\Addressbook\addressbook.py.py", line 53, in AddressRW
-
adFile = file('address.txt', 'r')
-
IOError: [Errno 2] No such file or directory: 'address.txt'
-
But I think this probably has something to do with the way the cmd prompt is setup...
I noticed a few things. Each 'try' must have an 'except' or a 'finally'. You should not use 'try...except' statements until your code is debugged. Some errors can be obfuscated. I assume 'Dill' was defined with 'from pickle import Unpickler as Dill'? This clouds the intent of your code when you ask for help. A pickle file is loaded like this: - pickle.Unpickler(open(file_name)).load()
Close a file like this: - f=open(file_name, 'w')
-
...do stuff...
-
f.close()
Notice the '()'. If an error occurs when the file is opened, it may never be written and you will end up with an empty file. -
f = open(file_name, 'w')
-
# dumps one object
-
pickle.Pickler(f).dump(object)
-
f.close()
-
# loads one object
-
pickle.Unpickler(open(file_name)).load()
Oh, I'm sorry. I'd completely forgotten the Dill thing. It's
import cPickle as Dill
Don't quite understand why I would only import Unpickler as I'm also dumping stuff..?
But yeah, I see what you’re saying. I can't believe I forgot to put the () after .close, and I didn't use Unpickler at all.
Anyway, thanks for the help, I'll probably re-write a bit of it tonight and see how it works.
Hmm...
pickle.Pickler(adFile).dump(adDict)
returns a value error. I'm still playing around with it. Didn't have much time to look at everything last night.
About the IOError, I'm thinking it has something to do with the security settings on this computer. Perhaps student users don't have permission to write the right thing, I don't know. Would anyone know what security settings might effect Pythons ability to write to files?
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Christian Tismer |
last post by:
Martin v. Löwis wrote:
> "Mark Hahn" <mark@hahnca.com> writes:
>
>
>>I don't understand how this could happen with pickle. Isn't it supposed to
>>stop when it runs into an object it has...
|
by: Jean-Luc |
last post by:
Hi
I am grafing pickling functionality on a big dictionary of object
instances and I am having numerous problems. I think understand the
notion of what can/can't be pickled and I know how to...
|
by: Nicolas Fleury |
last post by:
Is it possible to use the pickle module to update an object instead of
creating a new instance?
Thx
Nicolas
|
by: Michael Hohn |
last post by:
Hi,
under python 2.2, the pickle/unpickle sequence incorrectly restores
a larger data structure I have.
Under Python 2.3, these structures now give an explicit exception from...
|
by: Mike P. |
last post by:
Hi all,
I'm working on a simulation (can be considered a game) in Python where I
want to be able to dump the simulation state to a file and be able to load
it up later. I have used the standard...
|
by: Gabriel Genellina |
last post by:
Hello
I want to convert from pickle format to python source code. That is,
given an existing pickle, I want to produce a textual representation
which, when evaluated, yields the original object...
|
by: Jim Lewis |
last post by:
Pickling an instance of a class, gives "can't pickle instancemethod
objects". What does this mean? How do I find the class method creating
the problem?
|
by: srinivasan srinivas |
last post by:
Hi,
I would like to know how to pickle a bound method??
Thanks,
Srini
Meet people who discuss and share your passions. Go to http://in.promos.yahoo.com/groups/bestofyahoo/
|
by: srinivasan srinivas |
last post by:
HI Peter,
It works will for instance and class methods. But it doesn't work for static methods.
Can you tel me how to pickle static methods as well??
Thanks,
Srini
----- Original Message ----...
|
by: IceMan85 |
last post by:
Hi to all, I have spent the whole morning trying, with no success to pickle an object that I have created.
The error that I get is : Can't pickle 'SRE_Match' object: <_sre.SRE_Match object at...
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |