In the interest of not rewriting an open source library, I want to treat a string of text as a file in python 3.
Suppose I have the file contents as a string:
not_a_file = 'there is a lot of blah blah in this so-called file'
I want to treat this variable, i.e. the contents of a file, as a path-like object that way I can use it in python's open()
function.
Here is a simple example which shows my dilemma:
not_a_file = 'there is a lot of blah blah in this so-called file'
file_ptr = open(not_a_file, 'r')
Clearly the example doesn't work because not_a_file
is not a path-like object. I don't want to write a file nor create any temporary directories for portability purposes.
With that said, I need is to solve this mystery:
not_a_file = 'there is a lot of blah blah in this so-called file'
... Something goes here ...
file_ptr = open(also_not_a_file, 'r')
What I've Tried So Far
I've looked into StringIO and tried using that as a path-like object and no dice:
import StringIO output = StringIO.StringIO() output.write('First line.\n') file_ptr = open(output,'r')
Well this doesn't work because StringIO isn't a path-like object.I've tried tempfile in a similar fashion with no success.
import tempfile tp = tempfile.TemporaryFile() tp.write(b'there is a lot of blah blah in this so-called file') open(tp,'r')
- Finally I tried mmap to see if I can write the string into memory and then open the memory pointer with
open
with no success.
Any help is appreciated! :-)
Edit 1: What I'm thinking of to possibly solve problem
So pathlib.PurePath
can work with open()
if PurePath is initialized to a file. Perhaps I can create an instance of a class that inherits PurePath and when read by open()
, it reads my string. Let me give an example:
from pathlib import PurePath
not_a_file = 'there is a lot of blah blah in this so-called file'
class Magic(PurePath):
def __init__(self, string_input):
self.file_content = string_input
PurePath.__init__(self, 'Something magical goes here')
#some more magic happens in this class
also_not_a_file = Magic(not_a_file)
fp = open(also_not_a_file,'r')
print(fp.readlines()) # 'there is a lot of blah blah in this so-called file'