Creating file of size x

Grant Edwards grante at visi.com
Mon Jun 6 19:57:09 EDT 2005


On 2005-06-06, Jan Danielsson <jan.danielsson at gmail.com> wrote:

>>>The problem is that the design I'm working on won't guarantee
>>>what order the blocks will be returned in -- so I need to be
>>>able to seek to block n's location and write the ckeck block.
>> 
>> Exactly.  And precisely how did that fail?
>
> It didn't -- but that's on my platform;

What sort of programmer are you?  If it works on your computer,
it's done, ship it!  ;)

> I have no idea how it'll work on another platform; which is
> why I wanted to be able to first create the file of the
> specified size, and then start writing to it.
>
>>>Next block could be m, where m < n. So, they aren't continous.
>> 
>> If you do a seek before each write, it doesn't matter.
>
> According to Python, posix, Linux, Windows, ANSI-C?

Good question.  The Python documentation for the file object's
seek method is mute on the the topic of seeking past EOF.  I
believe the observed behavior is required by POSIX, SVr4, and
BSD for lseek() (which, I presume, is what Python calls on
those platforms).  That should have you covered for all of the
Linux and Linux-like OSes (included Mac OS X).

Under Win32, I don't know if there's an lseek() or what it
does.

I would guess that whoever wrote the file object's seek()
method went to some effort to make sure it works the same on
all platforms.

As somebody else pointed out, the following should work:

  f = file('name','wb')
  f.write('\x00' * requiredFileLength)

Then just do f.seek()/f.write() as the blocks come in.

-- 
Grant Edwards                   grante             Yow!  Sometime in 1993
                                  at               NANCY SINATRA will lead a
                               visi.com            BLOODLESS COUP on GUAM!!



More information about the Python-list mailing list