Creating file of size x
7 answers - 75 bytes -

Hello all,
Is there any way to create a file with a specified size?
No.1 | | 204 bytes |
| 
Jan Danielsson wrote:
Is there any way to create a file with a specified size?
What do you want to put in the file? you've answered that
question, the solution should present itself.
No.2 | | 590 bytes |
| 
Erik Max Francis wrote:
>Is there any way to create a file with a specified size?
What do you want to put in the file? you've answered that
question, the solution should present itself.
Check blocks from an FEC-encoder (Freenet, more specifically).
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. Next block could be m,
where m < n. So, they aren't continous.
No.3 | | 243 bytes |
| 
2005-06-06, Jan Danielsson <jan.danielsson (AT) gmail (DOT) comwrote:
Is there any way to create a file with a specified size?
Sure:
1) a new file for writing.
2) Seek to "specified size"-1.
3) Write one byte.
No.4 | | 799 bytes |
| 
2005-06-06, Jan Danielsson <jan.danielsson (AT) gmail (DOT) comwrote:
Erik Max Francis wrote:
Is there any way to create a file with a specified size?
>
>What do you want to put in the file? you've answered that
>question, the solution should present itself.
>
Check blocks from an FEC-encoder (Freenet, more specifically).
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?
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.
No.5 | | 1152 bytes |
| 
Grant Edwards wrote:
Is there any way to create a file with a specified size?
What do you want to put in the file? you've answered that
question, the solution should present itself.
>>
>>Check blocks from an FEC-encoder (Freenet, more specifically).
>>
>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; 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?
No.6 | | 715 bytes |
| 
Jan Danielsson wrote:
Is there any way to create a file with a specified size?
Besides the simple
def make_empty_file(filename, size):
f = open(filename, "wb")
f.write("\0" * size)
f.close()
?
If the file is large, try (after testing and fixing any
bugs):
def make_empty_file(filename, size, block = 32*1024):
f = open(filename, "wb")
written = 0
s = "\0" * block
for i in range(size//block):
f.write(s)
remainder = size%block
f.write(s[:remainder])
f.close()
As Grant Edwards pointed out, you can do a seek(size-1)
but I don't know if it's fully portable.
Andrew
dalke (AT) dalkescientific (DOT) com
No.7 | | 1792 bytes |
| 
2005-06-06, Jan Danielsson <jan.danielsson (AT) gmail (DOT) comwrote:
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 EF. I
believe the observed behavior is required by PSIX, 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 Ses (included Mac S 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.