How to read from byte object in Python

Viewed 475

I am able to tar a file successfully on the fly using subprocess.run, but I was not able to send the tar file using send_file option due to the following error:

AttributeError: 'bytes' object has no attribute 'read'

Using subprocess.Popen I was able to tar and send files successfully which are small in size (barely upto 1GB).But there is a problem while taring the files up to 4GB with subprocess.Popen, as subprocess.Popen is using memory to tar.

So I have moved to subprocess.run...but this is failing with the above error.

Please find the code snippet:

process = subprocess.run(['tar', '-cvf', '-', test_dir], stdout=subprocess.PIPE)
send_file(process.stdout, as_attachment=True, attachment_filename=test_dir.tar)

send_file is from flask framework.

Would appreciate any help with the above.

1 Answers

AttributeError: 'bytes' object has no attribute 'read

This mean ability to .read is expected, as you already have bytes it should be enough to use io.BytesIO from io built-in module like so

import io

...

process = subprocess.run(['tar', '-cvf', '-', test_dir], stdout=subprocess.PIPE)
data = process.stdout
send_file(io.BytesIO(data), as_attachment=True, attachment_filename=test_dir.tar)
Related