By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
457,876 Members | 1,119 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 457,876 IT Pros & Developers. It's quick & easy.

Creating file of size x

P: n/a
Hello all,

Is there any way to create a file with a specified size?
Jul 19 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
Jan Danielsson wrote:
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.

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
San Jose, CA, USA && 37 20 N 121 53 W && AIM erikmaxfrancis
All the people in my neighborhood turn around and get mad and sing
-- Public Enemy
Jul 19 '05 #2

P: n/a
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? 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.
Jul 19 '05 #3

P: n/a
On 2005-06-06, Jan Danielsson <ja************@gmail.com> wrote:
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.

--
Grant Edwards grante Yow! I just put lots of
at the EGG SALAD in the SILK
visi.com SOCKS --
Jul 19 '05 #4

P: n/a
On 2005-06-06, Jan Danielsson <ja************@gmail.com> wrote:
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? 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.


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.

--
Grant Edwards grante Yow! I'm also against
at BODY-SURFING!!
visi.com
Jul 19 '05 #5

P: n/a
Grant Edwards wrote:
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.


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?
Jul 19 '05 #6

P: n/a
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
da***@dalkescientific.com

Jul 19 '05 #7

P: n/a
On 2005-06-06, Jan Danielsson <ja************@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!!
Jul 19 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.