Creating file of size x

E

Erik Max Francis

Jan said:
Is there any way to create a file with a specified size?

What do you want to put in the file? Once you've answered that
question, the solution should present itself.
 
J

Jan Danielsson

Erik said:
What do you want to put in the file? Once 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.
 
G

Grant Edwards

Is there any way to create a file with a specified size?

Sure:

1) Open a new file for writing.
2) Seek to "specified size"-1.
3) Write one byte.
 
G

Grant Edwards

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.
 
J

Jan Danielsson

Grant said:
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.
If you do a seek before each write, it doesn't matter.

According to Python, posix, Linux, Windows, ANSI-C?
 
A

Andrew Dalke

Jan said:
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
(e-mail address removed)
 
G

Grant Edwards

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.


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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,241
Messages
2,571,219
Members
47,850
Latest member
StewartTha

Latest Threads

Top