Creates a file with some gibberish inside, returning the md5sum of that
file. File path and length are specified as function arguments.
'''
- f = open(path, 'w')
- s = string.ascii_lowercase + string.digits + "\n"
- if len(s) < length:
- s += s*int(length/len(s))
- data = s[:length]
- f.write(data)
- f.close()
+ data = string.ascii_lowercase + string.digits + "\n"
+
+ # determine how often need to repeat data and how much part of data is
+ # left in the end to fill file up to length
+ n_blocks, remainder = divmod(length, len(data))
+ with open(path, 'w') as write_handle:
+ for _ in range(n_blocks):
+ write_handle.write(data)
+ write_handle.write(data[:remainder])
return self.md5sum(path)
def md5sum(self, filename):