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

Hugo Arts hugo.yoshi at gmail.com
Sat Jul 17 14:41:17 CEST 2010


On Sat, Jul 17, 2010 at 2:01 PM, Richard D. Moores <rdmoores at gmail.com> 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?
>

You are not, and it would have been quite trivial to verify that:

$ python
Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)
[GCC 4.4.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> f = open("test.txt", 'w')
>>> f.write('hello')
>>> f.write('world\n')
>>> f.close()
>>>
$ cat test.txt
helloworld
$

As you can see, two subsequent writes will end up on a single line. If
you want a newline character, you have to specify it yourself, like I
did after the second write. So you can generate your large number in
small pieces, no problem at all.

Hugo


More information about the Tutor mailing list