[Tutor] A file containing a string of 1 billion random digits.

Mark Lawrence breamoreboy at yahoo.co.uk
Sat Jul 17 15:01:21 CEST 2010


On 17/07/2010 13:01, Richard D. Moores wrote:
> That's the goal of the latest version of my script at
> <http://tutoree7.pastebin.com/5XYaaNfp>. The best I've been able to do
> so far is a file with 800 million digits.
>
> But it seems the writing of 800 million digits is the limit for the
> amount of memory my laptop has (4 GB). So my question is, how can I do
> this differently? I'm pretty brand new to opening and writing files.
> Here, I can't write many shorter lines, because the end result I seek
> is one long string. But am I correct?
>
> I'd appreciate any advice.
>
> BTW line 29 was added after getting the outputs noted at the bottom of
> the script. Using close() does seem to shorten the time it takes for
> my laptop to become usable again, but it's not decisive. Sometimes I
> have to reboot in order to get healthy again. (64-bit Vista).
>
> BTW2 It's probably not obvious why the list comprehension (line 19)
> has random.choice(d) where d is '0123456789'. Without that, the random
> ints of 1000 digits would never begin with a '0'. So I give them a
> chance to by prefixing one random digit using choice(d), and cutting
> the length of the rest from 1000 to 999.
>
> Dick
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor
>

As an alternative to the suggestions given so far, why not open the file 
for write in binary mode, i.e. use 'wb' instead of 'w', and don't bother 
converting to strings?  Then when reading the file use 'rb' instead of 'r'.

HTH.

Mark Lawrence.



More information about the Tutor mailing list