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

bob gailer bgailer at gmail.com
Sat Jul 17 15:34:06 CEST 2010


On 7/17/2010 9:01 AM, Mark Lawrence wrote:

[snip]
>
> 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'.

You can only write strings to files. See 6.9 in the documentation:

file.write(/str/)
    Write a string to the file

b mode only affects how line ends are handled. See 2. Built-in Functions:

The default is to use text mode, which may convert '\n' characters to a 
platform-specific representation on writing and back on reading. Thus, 
when opening a binary file, you should append 'b' to the /mode/ value to 
open the file in binary mode, which will improve portability. (Appending 
'b' is useful even on systems that don't treat binary and text files 
differently, where it serves as documentation.)


-- 
Bob Gailer
919-636-4239
Chapel Hill NC

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20100717/7c8e870b/attachment.html>


More information about the Tutor mailing list