Correctly open files in binary mode on Windows, and reuse filename

This commit is contained in:
Frédéric Péters 2010-07-22 10:57:17 +02:00
parent 99032a5dec
commit 969d3b1fae
1 changed files with 13 additions and 4 deletions

View File

@ -9,14 +9,23 @@ import random
filename = sys.argv[1]
conn = httplib.HTTPConnection("ootest.pcf.be")
conn.request("POST", "/preview/odt", body=file(filename))
body = file(filename, 'rb').read()
conn.request("POST", "/preview/odt", body=body)
response = conn.getresponse()
data = response.read()
conn.close()
output = os.path.join(tempfile.gettempdir(), '%s.odt' % random.randint(0, 10000))
fd = file(output, 'wb')
output_filename = os.path.splitext(os.path.basename(filename))[0]
output_filename = os.path.join(tempfile.gettempdir(), output_filename)
if os.path.exists(output_filename + '.odt'):
i = 1
while os.path.exists(output_filename + '-%s.odt'%i):
i += 1
output_filename += '-%s' % i
output_filename += '.odt'
fd = file(output_filename, 'wb')
fd.write(data)
fd.close()
os.system('start %s' % output)
os.system('start %s' % output_filename)