Create n number of files with n bytes - Python (Load testing program)

30 views
Skip to first unread message

Justin

unread,
Aug 26, 2008, 7:29:41 AM8/26/08
to Open Projects For All
Note: For the latest build, always check the last post in this series

Author:Justin
Date: 26 August 2008
Version: build -3

Summary : program to test various load balancing features
Aim: the program aims to create a user specified number of files in
the current directory with a pre defined size.

Future enahancements: Timer functions, to accurately measure how much
times it takes
===========================================================================
Code
---------


#creates files which are 100 kb
# 1 MB = 1048576

for i in range(10):
st = 'X'
somebignumber = 102400
fh = open('junk' + str( i) ,'wb')
for n in xrange(somebignumber):
fh.write(st)
fh.close()


else:
print '10 files created, each of 100 KB'

Justin

unread,
Aug 26, 2008, 8:07:57 AM8/26/08
to Open Projects For All
NEW IN THIS VERSION
-timer capability added

TO DO
- network file creation/copy should be possible

==============================================

Code
-----------


#creates files which are 1 mb
# 1 MB = 1048576

#PROGRAMMER NOTES
#to measure time taken for python code snippets, check out
#http://www.python.org/doc/2.4/lib/module-timeit.html

#For more info on using datetime check out
#http://linux.byexamples.com/archives/364/python-manipulate-date-and-
time-variables/

import time

start = time.clock()

for i in range(50):
st = 'X'
somebignumber = 102400
fh = open('junk' + str( i) ,'wb')
for n in xrange(somebignumber):
fh.write(st)
fh.close()


else:
end = time.clock()

print "10 files created, each of 100 KB, took about ",end - start , "
secs."

Justin

unread,
Aug 26, 2008, 9:08:17 AM8/26/08
to Open Projects For All
DELETE THE LAST LINE FROM A FILE
==================================

for i in range(10):
fileHandle = open('junk' + str( i) ,'r')
data_list = fileHandle.readlines()
fileHandle.close()

del data_list[len(data_list)-1:len(data_list)]
fileHandle = open('junk' + str( i) ,'w')
fileHandle.write ("".join(data_list))
#list must be converted to string before writing to text file, hence
we use s<string>. join function

else:
print "Completed deleting the last line from " , i , "files"

Justin

unread,
Aug 26, 2008, 9:09:20 AM8/26/08
to Open Projects For All
DELETE 'N' NUMBER OF FILES FROM A DIRECTORY
(shows the final status too, i.e total number of files that was
deleted)

import os
count = 0



for i in range(10):
count = count + 1
try:
os.remove('junk' + str( i) )
except OSError:
count = count - 1
print 'Cannot delete file. Error occurred while trying to delete
file.'
else:
print"Total" , count , "files removed."

Justin

unread,
Aug 26, 2008, 9:11:17 AM8/26/08
to Open Projects For All


hi guys,

I would love to post an update here. If you like to create files in a
network folder, add the following line

fh = open(r'\\192.168.100.3\New Folder\junk' + str( i) ,'wb')

#^^ Dont forget the r
with regards
justin
Reply all
Reply to author
Forward
0 new messages