2

For testing purposes, I want to create a little utility which will prompt me for the size of a file I want to create and the location I want to send it to, then record the amount of time it takes to create the file, before logging the results on an Excel spreadsheet. So far I have this:

import time
import pythoncom
from win32com.client import Dispatch

#create file
myPath = input('Where do you want to write the file?')
size_MB = int(input('What sized file do you want to test with? (MB)'))
size_B = size_MB * 1024 * 1024
fName = '\pydatafile'
#start timer
start = time.clock()
f = open(myPath + fName,'w')
f.write("\x00" * size_B)
f.close()

#how much time it took
elapsed = (time.clock() -start)
print ("It took", elapsed, "seconds to write the", size_B, "MB file")
#I'll sort something out to work out Mbps when the rest is ready

#record results on Excel
xl = Dispatch('Excel.Application')
xl.visible= 0
wb = xl.Workbooks.Add(r'C:\Users\ryansinfield\Desktop\Book1.xlsm')
ws = wb.Worksheets(1)

#loop until empty cell is found in column
col = 1
row = 1
empty = False

while not empty:
    val = ws.Cells(row,col).value
    print("Looking for next available cell to write to...")
    if val == None:
        print("Writing result to cell")
        ws.Cells(row,col).value = Mbps
        empty = True
    row += 1

xl.Run('Save')
xl.Quit()

pythoncom.CoUninitialize()

This creates a file to the specified location ok, but the file creation is instant. It doesn't matter what size I specify for the file it will always appear as 0 bytes and take microseconds to create.

Is there any way I can create an actual file which will allow me to test the LAN speed more effectively?

EDIT: It seems to be working now in terms of speed. I don't know why it just is, maybe I was being a numpty. However there's something I want to add. Is it possible to delete the blank file once the test has been run?

DisplayName
  • 3,093
  • 5
  • 35
  • 42
ryansin
  • 1,735
  • 2
  • 26
  • 49
  • Try calling `f.close()` after `write`, I think the problem is that the buffer is not flushed to disk yet when you measure the time. – fjarri Sep 16 '13 at 16:05
  • Also note that f will be 'None' since you are assigning the result of file.write() to f, which has no normal return. – gregb212 Sep 16 '13 at 16:07
  • Bogdan, when I call f.close() I am told that "'int' object has no attribute 'close'" – ryansin Sep 16 '13 at 16:16
  • Ah I see, I've amended the question now. Thanks for the input – ryansin Sep 16 '13 at 16:22
  • What version of Python are you using? – thegrinner Oct 11 '13 at 20:59
  • You probably should use the [with statement](http://effbot.org/zone/python-with-statement.htm) instead of manually closing the file. It will guarantee the file handle gets closed even if there's an exception. – kojiro Oct 11 '13 at 21:02

1 Answers1

0

A couple things you might find interesting:

  • The built in os module has some nice functions for doing filesystem stuff:

    • Using the os.path module you don't have to worry about platform-dependant syntax or backslashes when dealing with paths.

    • You can use os.remove to delete a file.

  • Because you're using Excel, I'm guessing you're on Windows: consider using 'wb' instead of 'w' when writing to a file because Windows deals with EOL characters differently than Unix.

  • "0.Df"%number can be used to format the floating point 'number with D decimal places.

Your code with these changes added in:

import os
import time
import pythoncom
from win32com.client import Dispatch

#create file
myDir = input('Where do you want to write the file?')
myPath = os.path.join(myDir, 'pydatafile')

size_MB = int(input('What sized file do you want to test with? (MB): '))
size_B = size_MB * 1024 * 1024

# create string to write
to_write = "\x00" * size_B

#start timer
start = time.clock()
f = open(myPath, 'wb')
f.write(to_write)
f.close()

#how much time it took
elapsed = time.clock() - start
print "It took %0.3f"%elapsed, "seconds to write the", size_MB, "MB file at a rate of %0.3f"%(size_MB/elapsed), "Mbps."

#record results on Excel
xl = Dispatch('Excel.Application')
xl.visible= 0
wb = xl.Workbooks.Add(r'C:\Users\ryansinfield\Desktop\Book1.xlsm')
ws = wb.Worksheets(1)

#loop until empty cell is found in column
col = 1
row = 1
empty = False

while not empty:
    val = ws.Cells(row,col).value
    print("Looking for next available cell to write to...")
    if val == None:
        print("Writing result to cell")
        ws.Cells(row,col).value = Mbps
        empty = True
    row += 1

xl.Run('Save')
xl.Quit()

pythoncom.CoUninitialize()

# Remove the file at the end
os.remove(myPath)
Community
  • 1
  • 1
crennie
  • 674
  • 7
  • 18
  • It looks like OP is using Python 3 (see the print statements), which replaces `input()` with the old `raw_input()` behavior. – thegrinner Oct 11 '13 at 21:00
  • That's probably true, the print()s are a good indication. I'll update my answer – crennie Oct 15 '13 at 15:59