I am trying to write a cStringIO buffer to disk. The buffer may represent a pdf, image or html file.
The approach I took seems a bit wonky, so I am open to alternative approaches as a solution as well.
def copyfile(self, destfilepath):
if self.datastream.tell() == 0:
raise Exception("Exception: Attempt to copy empty buffer.")
with open(destfilepath, 'wb') as fp:
shutil.copyfileobj(self.datastream, fp)
self.__datastream__.close()
@property
def datastream(self):
return self.__datastream__
#... inside func that sets __datastream__
while True:
buffer = response.read(block_sz)
self.__datastream__.write(buffer)
if not buffer:
break
# ... etc ..
test = Downloader()
ok = test.getfile(test_url)
if ok:
test.copyfile(save_path)
I took this approach because I don't want to start writting data to disk until I know I have successfully read the entire file and that it's a type I am interested in.
After calling copyfile() the file on disk is always zero bytes.