[Tutor] how to read and write to a file
Alexander
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)
>
> file1.close()
> file2.close()
>
> Any help would be greatly appreciated.
>
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>
>
Hi Ken. If you just want to read and write from a text file then you don't
need to pickle.
For example,
(the file info.txt exists)
>>>fh = open ( 'info.txt', 'w' )
>>>fh.write ( 'peter piper picked a pack of pickled peppers.' )
>>>fh.close()
>>>fr = open ( 'info.txt', 'r')
>>>fr.readline()
'peter piper picked a pack of pickled peppers.'
>>>fr.close()
or whatever.
But do you have a need to use pickling?
--
Alexander
7D9C597B
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120125/bca17c31/attachment.html>
More information about the Tutor
mailing list