[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Creating a file with $SIZE

iPerfect

3/12/2008 10:32:00 AM

Hi All,

I would like create files of different size, taking size as user
input. I need to check the data transfer rates from one network to
another . In order to do this I will have to create files of diff size
and work out. I am new to Python

Thanks in advance.

KK
17 Answers

Chris

3/12/2008 10:53:00 AM

0

On Mar 12, 12:32 pm, "k.i.n.g." <nkanthiki...@gmail.com> wrote:
> Hi All,
>
> I would like create files of different size, taking size as user
> input. I need to check the data transfer rates from one network to
> another . In order to do this I will have to create files of diff size
> and work out. I am new to Python
>
> Thanks in advance.
>
> KK

Welcome to Python.

If you just want to create files with random junk from the user input
then maybe something along these lines would help:

import sys, random

def random_junk(number_of_characters):
tmp = []
while number_of_characters:
tmp.append(random.randint(0, 127))
number_of_characters -= 1
return ''.join(map(str,tmp))

if len(sys.argv) < 2:
sys.exit('Usage:python %s <space seperated
filesizes>'%sys.argv[0])

for each_argv in sys.argv[1:]:
output_file = open(each_argv,'wb').write(random_junk(each_argv))

Chris

3/12/2008 11:18:00 AM

0

On Mar 12, 12:52 pm, Chris <cwi...@gmail.com> wrote:
> On Mar 12, 12:32 pm, "k.i.n.g." <nkanthiki...@gmail.com> wrote:
>
> > Hi All,
>
> > I would like create files of different size, taking size as user
> > input. I need to check the data transfer rates from one network to
> > another . In order to do this I will have to create files of diff size
> > and work out. I am new to Python
>
> > Thanks in advance.
>
> > KK
>
> Welcome to Python.
>
> If you just want to create files with random junk from the user input
> then maybe something along these lines would help:
>
> import sys, random
>
> def random_junk(number_of_characters):
>     tmp = []
>     while number_of_characters:
>         tmp.append(random.randint(0, 127))
>         number_of_characters -= 1
>     return ''.join(map(str,tmp))
>
> if len(sys.argv) < 2:
>     sys.exit('Usage:python %s <space seperated
> filesizes>'%sys.argv[0])
>
> for each_argv in sys.argv[1:]:
>     output_file = open(each_argv,'wb').write(random_junk(each_argv))

Sorry, meant

def random_junk(number_of_characters):
tmp = []
while number_of_characters:
tmp.append(chr(random.randint(0, 127)))
number_of_characters -= 1
return ''.join(tmp)

iPerfect

3/12/2008 11:38:00 AM

0


I think I am not clear with my question, I am sorry. Here goes the
exact requirement.

We use dd command in Linux to create a file with of required size. In
similar way, on windows I would like to use python to take the size of
the file( 50MB, 1GB ) as input from user and create a uncompressed
file of the size given by the user.

ex: If user input is 50M, script should create 50Mb of blank or empty
file

Thank you

Robert Bossy

3/12/2008 12:52:00 PM

0

k.i.n.g. wrote:
> I think I am not clear with my question, I am sorry. Here goes the
> exact requirement.
>
> We use dd command in Linux to create a file with of required size. In
> similar way, on windows I would like to use python to take the size of
> the file( 50MB, 1GB ) as input from user and create a uncompressed
> file of the size given by the user.
>
> ex: If user input is 50M, script should create 50Mb of blank or empty
> file
>
def make_blank_file(path, size):
f = open(path, 'w')
f.seek(size - 1)
f.write('\0')
f.close()

I'm not sure the f.seek() trick will work on all platforms, so you can:

def make_blank_file(path, size):
f = open(path, 'w')
f.write('\0' * size)
f.close()

Cheers,
RB

Matt Nordhoff

3/12/2008 1:09:00 PM

0

Robert Bossy wrote:
> k.i.n.g. wrote:
>> I think I am not clear with my question, I am sorry. Here goes the
>> exact requirement.
>>
>> We use dd command in Linux to create a file with of required size. In
>> similar way, on windows I would like to use python to take the size of
>> the file( 50MB, 1GB ) as input from user and create a uncompressed
>> file of the size given by the user.
>>
>> ex: If user input is 50M, script should create 50Mb of blank or empty
>> file
>>
> def make_blank_file(path, size):
> f = open(path, 'w')
> f.seek(size - 1)
> f.write('\0')
> f.close()
>
> I'm not sure the f.seek() trick will work on all platforms, so you can:
>
> def make_blank_file(path, size):
> f = open(path, 'w')
> f.write('\0' * size)
> f.close()

I point out that a 1 GB string is probably not a good idea.

def make_blank_file(path, size):
chunksize = 10485760 # 10 MB
chunk = '\0' * chunksize
left = size
fh = open(path, 'wb')
while left > chunksize:
fh.write(chunk)
left -= chunksize
if left > 0:
fh.write('\0' * left)
fh.close()

> Cheers,
> RB
--

Robert Bossy

3/12/2008 1:44:00 PM

0

Matt Nordhoff wrote:
> Robert Bossy wrote:
>
>> k.i.n.g. wrote:
>>
>>> I think I am not clear with my question, I am sorry. Here goes the
>>> exact requirement.
>>>
>>> We use dd command in Linux to create a file with of required size. In
>>> similar way, on windows I would like to use python to take the size of
>>> the file( 50MB, 1GB ) as input from user and create a uncompressed
>>> file of the size given by the user.
>>>
>>> ex: If user input is 50M, script should create 50Mb of blank or empty
>>> file
>>>
>>>
>> def make_blank_file(path, size):
>> f = open(path, 'w')
>> f.seek(size - 1)
>> f.write('\0')
>> f.close()
>>
>> I'm not sure the f.seek() trick will work on all platforms, so you can:
>>
>> def make_blank_file(path, size):
>> f = open(path, 'w')
>> f.write('\0' * size)
>> f.close()
>>
>
> I point out that a 1 GB string is probably not a good idea.
>
> def make_blank_file(path, size):
> chunksize = 10485760 # 10 MB
> chunk = '\0' * chunksize
> left = size
> fh = open(path, 'wb')
> while left > chunksize:
> fh.write(chunk)
> left -= chunksize
> if left > 0:
> fh.write('\0' * left)
> fh.close()
>
Indeed! Maybe the best choice for chunksize would be the file's buffer
size... I won't search the doc how to get the file's buffer size because
I'm too cool to use that function and prefer the seek() option since
it's lighning fast regardless the size of the file and it takes near to
zero memory.

Cheers,
RB

cokofreedom

3/12/2008 3:28:00 PM

0

On Mar 12, 2:44 pm, Robert Bossy <Robert.Bo...@jouy.inra.fr> wrote:
> Matt Nordhoff wrote:
> > Robert Bossy wrote:
>
> >> k.i.n.g. wrote:
>
> >>> I think I am not clear with my question, I am sorry. Here goes the
> >>> exact requirement.
>
> >>> We use dd command in Linux to create a file with of required size. In
> >>> similar way, on windows I would like to use python to take the size of
> >>> the file( 50MB, 1GB ) as input from user and create a uncompressed
> >>> file of the size given by the user.
>
> >>> ex: If user input is 50M, script should create 50Mb of blank or empty
> >>> file
>
> >> def make_blank_file(path, size):
> >> f = open(path, 'w')
> >> f.seek(size - 1)
> >> f.write('\0')
> >> f.close()
>
> >> I'm not sure the f.seek() trick will work on all platforms, so you can:
>
> >> def make_blank_file(path, size):
> >> f = open(path, 'w')
> >> f.write('\0' * size)
> >> f.close()
>
> > I point out that a 1 GB string is probably not a good idea.
>
> > def make_blank_file(path, size):
> > chunksize = 10485760 # 10 MB
> > chunk = '\0' * chunksize
> > left = size
> > fh = open(path, 'wb')
> > while left > chunksize:
> > fh.write(chunk)
> > left -= chunksize
> > if left > 0:
> > fh.write('\0' * left)
> > fh.close()
>
> Indeed! Maybe the best choice for chunksize would be the file's buffer
> size... I won't search the doc how to get the file's buffer size because
> I'm too cool to use that function and prefer the seek() option since
> it's lighning fast regardless the size of the file and it takes near to
> zero memory.
>
> Cheers,
> RB

But what platforms does it work on / not work on?

Marco Mariani

3/12/2008 4:45:00 PM

0

Robert Bossy wrote:

> Indeed! Maybe the best choice for chunksize would be the file's buffer
> size... I won't search the doc how to get the file's buffer size because
> I'm too cool to use that function and prefer the seek() option since
> it's lighning fast regardless the size of the file and it takes near to
> zero memory.

And makes a hole in the file, I suppose, hence the fragmentation.

The OP explicitly asked for an uncompressed file.

David Robinow

3/13/2008 3:08:00 AM

0

On Mar 12, 7:37 am, "k.i.n.g." <nkanthiki...@gmail.com> wrote:
> We use dd command in Linux to create a file with of required size.
If you just want to get your work done, you might consider the cygwin
dd command.
Learning to write python is a worthwhile endeavour in any case.

iPerfect

3/13/2008 5:45:00 AM

0

On Mar 13, 8:07 am, "drobi...@gmail.com" <drobi...@gmail.com> wrote:
> On Mar 12, 7:37 am, "k.i.n.g." <nkanthiki...@gmail.com> wrote:> We use dd command in Linux to create a file with of required size.
>
> If you just want to get your work done, you might consider the cygwin
> dd command.
> Learning to write python is a worthwhile endeavour in any case.

While I just started learning programming/python, I got this
requirement at my workplace. I want to learn python than just get
things done.

Thank you all for the solutions, I will try them and let you all know
about my results.