Size in byte of a string ?

Eric Brunel eric.brunel at pragmadev.com
Mon Jul 29 07:36:37 EDT 2002


Shagshag13 wrote:

> [sorry for the previous incomplete post]
> 
> hello,
> 
> i had the following problem : i had huge files > 1,5 go. each of theses
> files contain some other small files and they are separate by a header. i
> need to do some kind of random access to the small files so i wish to
> write a script which would build some kind of a table of content for
> theses huges files.
> 
> i would like to have something like
> samllfile_header_name_i : start_at_byte : end_at_byte
> 
> and i will do :
> f.seek(start_at_byte)
> samllfile_header_name_content = f.read(end_at_byte)
> 
> but to do this i need to be able at the building time to know how much
> bytes take a string ?

Eeeer, len(string).... But you should open your huge file as binary, not 
text:

f = open(fileName, 'rb')

so that everything in the file will be kept untouched. With this, you will 
actually get '\r\n' at the end of each line instead of a single '\n'

HTH
-- 
- Eric Brunel <eric.brunel at pragmadev.com> -
PragmaDev : Real Time Software Development Tools - http://www.pragmadev.com



More information about the Python-list mailing list