[Tutor] how to read and write to a file
Joel Goldstick
joel.goldstick at gmail.com
Wed Jan 25 15:54:43 CET 2012
On Wed, Jan 25, 2012 at 9:14 AM, Alexander <rhettnaxel at gmail.com> wrote:
> On Wed, Jan 25, 2012 at 8:32 AM, ken brockman <krush1954 at yahoo.com> wrote:
>>
>>
>> ________________________________
>> From: Alexander <rhettnaxel at gmail.com>
>> To: ken brockman <krush1954 at yahoo.com>
>> Cc: "tutor at python.org" <tutor at python.org>
>> Sent: Wednesday, January 25, 2012 7:38 AM
>> Subject: Re: [Tutor] how to read and write to a file
>>
>>
>>
>> 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
>> --------------------------------------------------------------------------
>>
>> Hey Alexander,
>> I had to try it before I went to sleep.
>> No good. I got an error msg. TypeError: must be str, not list.
>> So I guess that may be why i had went with pickling. I needed something that would work with a list. Unless there is some other way?
>> Thanks again for taking the time to help out.
>>
>> Ken
>>
>>
> Ken, pickling is used when one wants to send information through a
> network or communicate with a database. Somebody else here knows more
> about pickling than I do. As for your list problem... I'm not exactly
> certain what you're trying to do. But I'm going with the idea that you
> have two files of information, one contains strings you like, and the
> other contains strings you dislike. And you want to read and write
> this information using Python.
>
>>>> like = [ 'orange', 'blue', 'red' ] #things I like
>>>> dislike = [ 'apples', 'bronze', 'bananas' ] #things I dislike
>>>> fh = open ( 'likes.txt', 'w' ) #let's open a file stream to write
>
> # fh is my shorthand for "file handle"
> #writing a list to a file stream:
>
>>>> for index in range( len( like )):
> fh.write( like[ index ] )
> fh.write ( '\n' ) #here i add a new line, maybe somebody else
> #knows a better way to avoid this?
>
>>>> fh.close()
>
> #now let's read that information into a list
>>>> fr = open ( 'info.txt' ) #I'm using python 3.2
>>>> mylistoflikes = fr.readlines()
>>>> mylistoflikes
> [ 'orange\n' , 'blue\n' , 'red\n' ]
>>>> fr.close()
>
> --
> Alexander
> 7D9C597B
> _______________________________________________
> Tutor maillist - Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
First, pickle is used to store data objects. If you just want to
store text, you just write it and read it back from your file. But
there are situations where you want to save an object. Maybe you want
to have it available to another program.
So, you nearly got through your example. You pickled your lists, and
stored them, but you didn't retrieve them.
Here is a link to review: http://wiki.python.org/moin/UsingPickle
Here is my version of your program. It worked for me. copy and paste
it in a file, and try it
-----------------------------------------------
import pickle
ArtyKlikes = (1,2,3)
ArtyKDislikes = (4,5,6)
file1 = open("ArtyKlikes.p", "ab") # likesList
file2 = open("ArtyDislikes.p", "ab") # dislikes
pickle.dump(ArtyKlikes, file1)
pickle.dump(ArtyKDislikes, file2)
file1.close()
file2.close()
ArtyLikes = pickle.load(open("ArtyKlikes.p", 'rb'))
ArtyDisLikes = pickle.load(open("ArtyDislikes.p", 'rb'))
print ArtyLikes
print ArtyDisLikes
--
Joel Goldstick
More information about the Tutor
mailing list