[Tutor] how to read and write to a file
rhettnaxel at gmail.com
Wed Jan 25 13:38:14 CET 2012
On Wed, Jan 25, 2012 at 7:19 AM, ken brockman <krush1954 at yahoo.com> wrote:
> I would like to write to and read from a file from python. I wanted to use
> the file to save input to the program in a list. I have been looking around
> and there seems to be several ways to go about it. I tried pickling, but am
> having an issue with it. What would be the correct way to accomplish this?
> I have tried several ways, but to no avail. I get no error msg. but the
> list isn't retaining the info. Is pickling even the best way to do it.
> file1 = open("ArtyKlikes.p", "ab") # likesList
> file2 = open("ArtyDislikes.p", "ab") # dislikes
> pickle.dump(likesList, file1)
> pickle.dump(dislikeList, file2)
> Any help would be greatly appreciated.
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
Hi Ken. If you just want to read and write from a text file then you don't
need to pickle.
(the file info.txt exists)
>>>fh = open ( 'info.txt', 'w' )
>>>fh.write ( 'peter piper picked a pack of pickled peppers.' )
>>>fr = open ( 'info.txt', 'r')
'peter piper picked a pack of pickled peppers.'
But do you have a need to use pickling?
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Tutor