stringio

Function to create in-memory zip file and return as http response

馋奶兔 提交于 2019-12-17 16:29:18
问题 I am avoiding the creation of files on disk, this is what I have got so far: def get_zip(request): import zipfile, StringIO i = open('picture.jpg', 'rb').read() o = StringIO.StringIO() zf = zipfile.ZipFile(o, mode='w') zf.writestr('picture.jpg', i) zf.close() o.seek(0) response = HttpResponse(o.read()) o.close() response['Content-Type'] = 'application/octet-stream' response['Content-Disposition'] = "attachment; filename=\"picture.zip\"" return response Do you think is correct-elegant-pythonic

Extracting a zipfile to memory?

99封情书 提交于 2019-12-17 10:38:17
问题 How do I extract a zip to memory? My attempt (returning None on .getvalue() ): from zipfile import ZipFile from StringIO import StringIO def extract_zip(input_zip): return StringIO(ZipFile(input_zip).extractall()) 回答1: extractall extracts to the file system, so you won't get what you want. To extract a file in memory, use the ZipFile.read() method. If you really need the full content in memory, you could do something like: def extract_zip(input_zip): input_zip=ZipFile(input_zip) return {name:

In Memory Zip File Python Error

偶尔善良 提交于 2019-12-13 19:25:14
问题 I'm trying to make an in-memory zip file in Python and upload it to Amazon S3. I've read the similar posts on the matter, but no matter what I try, Windows and Linux (RHEL5) cannot open it (it's corrupt). Here's the code I'm running: f_redirects = StringIO() f_links = StringIO() f_metadata = StringIO() # Write to the "files" zip_file = StringIO() zip = zipfile.ZipFile(zip_file, 'a', zipfile.ZIP_DEFLATED, False) zip.writestr('redirects.csv', f_redirects.getvalue()) zip.writestr('links.csv', f

relative seek for io.StringIO in python3

ぐ巨炮叔叔 提交于 2019-12-12 18:24:43
问题 I am trying to refactor a python 2 package for use with python-3.x. The package uses StringIO.StringIO under python 2 and makes some use of the object's relative seek method, with statements like flob.seek(-1, 1) . Unfortunately, the seek method of the corresponding io.StringIO object in python 3 does not support relative seeks, so the code raises OSError: Can't do nonzero cur-relative seeks when trying to execute that statement. What is the best way to refactor modules containing these calls

scikit-image save image to a bytestring

懵懂的女人 提交于 2019-12-12 17:18:36
问题 I'm using scikit-image to read an image: img = skimage.io.imread(filename) After doing some manipulations to img , I'd like to save it to an in-memory file (a la StringIO) to pass off to another function, but it looks like skimage.io.imsave requires a filename, not a file handle. I'd like to avoid hitting the disk ( imsave followed by read from another imaging library) if at all possible. Is there a nice way to get imsave (or some other scikit-image-friendly function) to work with StringIO ?

How does one add string to tarfile in Python3

巧了我就是萌 提交于 2019-12-12 11:24:52
问题 I have problem adding an str to a tar arhive in python. In python 2 I used such method: fname = "archive_name" params_src = "some arbitrarty string to be added to the archive" params_sio = io.StringIO(params_src) archive = tarfile.open(fname+".tgz", "w:gz") tarinfo = tarfile.TarInfo(name="params") tarinfo.size = len(params_src) archive.addfile(tarinfo, params_sio) Its essentially the same what can be found in this here. It worked well. However, going to python 3 it broke and results with the

How can I send a StringIO via FTP in python 3?

老子叫甜甜 提交于 2019-12-12 10:58:51
问题 I want to upload a text string as a file via FTP. import ftplib from io import StringIO file = StringIO() file.write("aaa") file.seek(0) with ftplib.FTP() as ftp: ftp.connect("192.168.1.104", 2121) ftp.login("ftp", "ftp123") ftp.storbinary("STOR 123.txt", file) This code returns an error: TypeError: 'str' does not support the buffer interface 回答1: This can be a point of confusion in python 3, especially since tools like csv will only write str , while ftplib will only accept bytes . You can

Python logging to StringIO handler

左心房为你撑大大i 提交于 2019-12-12 07:25:49
问题 I have a python test in which I want to test if the logging works properly. For example I have a function that creates a user and at the end the logging writes to log file the response. logger = logging.getLogger('mylogger') logger.setLevel(logging.DEBUG) handler = logging.handlers.WatchedFileHandler('mylogfile.log') formatter = logging.Formatter('%(asctime)s: %(message)s', '%d/%b/%Y:%H:%M:%S %z') handler.setFormatter(formatter) logger.addHandler(handler) logger.info('Some log text') In my

Python: Optimizing Images in Memory (StringIO & POpen with jpegoptim)

喜欢而已 提交于 2019-12-11 12:47:51
问题 I'm trying to compress images without touching disk using the STDIN version of various libraries(jpegoptim in this example). This code does not return an optimized(jpegoptim compressed) image. Can someone please help or explain why this usage of Popen() with a StringIO.StringIO() object does not return the optimized version of the image? If I save the file to disk, it works just fine. import sys import urllib2 as urllib import StringIO from subprocess import Popen, PIPE, STDOUT fp = urllib

Python StringIO - selectively place data into stdin

99封情书 提交于 2019-12-11 10:01:13
问题 We're using a bit of compiled python code that we don't have the source to. The code prompts for user input and we're trying to automate that portion. Basically asks for username, password, then some various questions depending on certain circumstances. I don't know whether the compiled function is using raw_input, input or something else. I've been able to use StringIO to replace stdin w/ the username and password and I can replace stdout with my own class and figure out which prompt is