0

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.

Baywatch
  • 423
  • 5
  • 17

1 Answers1

0

whoops!

I forgot to reset the stream position before trying to read from it; so it was reading from the end, hence the zero bytes. Moving the cursor to the beginning resolves the issue.

def copyfile(self, destfilepath):
    if self.datastream.tell() == 0:
        raise Exception("Exception: Attempt to copy empty buffer.")
    self.__datastream__.seek(0)  # <-- RESET POSITION TO BEGINNING
    with open(destfilepath, 'wb') as fp:
        shutil.copyfileobj(self.datastream, fp)
    self.__datastream__.close()
Baywatch
  • 423
  • 5
  • 17