0

I am trying tp Create a directory in memory in python, zip it and return it as bytes.

I have used the following tools:

def some_func(data):
   some_large_number = 10000000000000000000000000
   f = tempfile.SpooledTemporaryFile(max_size = some_large_number, mode = 'w+b'. dir = 'temp/')

   with f as file:
      the_directory = data.to_directory()
      a = shutil.make_archive(file, 'zip', the_directory)
      return pickle.dumps(a)

However, I get the following error message: TypeError: Expected str, bytes, or os.Pathlike object, not spooledTemporaryFile

I have also tries 'return io.BytesIO(a)' and 'io.StringIO(a)' yet get the same error message. Not sure how to return the object in the approptiate type instead of spooledTemporaryFile?

I also tried the following:

def some_func(data):
   some_large_number = 10000000000000000000000000
   f = tempfile.SpooledTemporaryFile(max_size = some_large_number, mode = 'w+b'. dir = 'temp/')

   with f as file:
      the_directory = data.to_directory()
      a = shutil.make_archive('final', 'zip', io.BytesIO(the_directory))
      return pickle.dumps(a)

and got the following error: TypeError: A bytes-like object is required not zarr_store

Note: the_director is a Zarr_store type object but I thought calling io.BytesIO() converts that to bytes?

  • The first argument is supposed to be the name of the archive file to create. `shutil.make_archive` can't write to an already opened file. Use the lower-level `zipfile` library. – Barmar Dec 20 '22 at 20:54
  • I'm not clear on what you're trying to do here. Are you trying to make an in-memory zip file and then pickle it? – President James K. Polk Dec 20 '22 at 20:55
  • I'm trying to return the directory as a zip file in memory and return it in bytes – user20816596 Dec 20 '22 at 21:00
  • @Barmar I updated the question with a new code snippet – user20816596 Dec 20 '22 at 21:13
  • 1
    I don't think Unix or Windows allow creating directories in memory by user applications. You need to mount a filesystem on a memory disk. – Barmar Dec 20 '22 at 21:17
  • Would there be no way of fixing `TypeError: expected str, bytes or os.path liek object, not SpooledTemporaryFile`? No way to convert to bytes? – user20816596 Dec 20 '22 at 21:53
  • 1
    It's not clear to me also. You are ambiguously referring the content of a zip archive on a directory as a 'directory'. Right? Then see [this question](https://stackoverflow.com/questions/18457678/python-write-in-memory-zip-to-file). See the answer of 'user3226167'. – relent95 Dec 21 '22 at 01:23
  • *...I'm trying to return the directory...* what directory? There is no directory! – President James K. Polk Dec 21 '22 at 02:07
  • @PresidentJamesK.Polk Sorry, I meant the zip file in bytes that holds the contents of the directory. Please note in my actual code `the_directory` is made using `data`. – user20816596 Dec 21 '22 at 15:00

0 Answers0