Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create in-memory file object

Tags:

python

io

I want to make a in-memory file to use in pygame mixer. I mean something like http://www.pygame.org/docs/ref/music.html#pygame.mixer.music.load which says load() method supports file object.

import requests from pygame import mixer  r = requests.get("http://example.com/some_small_file.mp3") in_memory_file = file(r.content) # something like this mixer.music.init() mixer.music.load(in_memory_file) mixer.music.play() 
like image 606
Fatih Kılıç Avatar asked Jun 21 '17 09:06

Fatih Kılıç


People also ask

How do you create a file object in Python?

To create a file object in Python use the built-in functions, such as open() and os. popen() . IOError exception is raised when a file object is misused, or file operation fails for an I/O-related reason. For example, when you try to write to a file when a file is opened in read-only mode.

What is in memory file in Python?

Reading MemoryFiles Like BytesIO , MemoryFile implements the Python file protocol and provides read() , seek() , and tell() methods. Instances are thus suitable as arguments for methods like requests.

What is an in memory file?

The distinguishing characteristic of memory files is that they exist solely in memory, occupying their own memory space separate from the data and index caches. Memory files satisfy the need for the creation and manipulation of temporary data or index files that are always memory resident and never touch disk.

What is file like object?

A file-like object is just a synonym for file-object. See Python Glossary. Show activity on this post. File-like objects are mainly StringIO objects, connected sockets and, well, actual file objects.


1 Answers

You are probably looking for BytesIO or StringIO classes from Python io package, both available in python 2 and python 3. They provide a file-like interface you can use in your code the exact same way you interact with a real file.

StringIO is used to store textual data:

import io  f = io.StringIO("some initial text data") 

BytesIO must be used for binary data:

import io  f = io.BytesIO(b"\x00\x00\x00\x00\x00\x00\x00\x00\x01\x01\x01\x01\x01\x01") 

To store MP3 file data, you will probably need the BytesIO class. To initialize it from a GET request to a server, proceed like this:

import requests from pygame import mixer import io  r = requests.get("http://example.com/somesmallmp3file.mp3") inmemoryfile = io.BytesIO(r.content)  mixer.music.init() mixer.music.load(inmemoryfile) mixer.music.play()  # This will free the memmory from any data inmemoryfile.close() 

Additional note: as both classes inherit from IOBase, they can be used as context manager with the with statement, so you don't need to manually call the close() method anymore:

import requests from pygame import mixer import io  r = requests.get("http://example.com/somesmallmp3file.mp3")  with io.BytesIO(r.content) as inmemoryfile:     mixer.music.init()     mixer.music.load(inmemoryfile)     mixer.music.play() 
like image 87
Antwane Avatar answered Sep 28 '22 22:09

Antwane