问题
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
openwith 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'
回答1:
You can create a temporary file and pass its name to open:
On Unix:
tp = tempfile.NamedTemporaryFile()
tp.write(b'there is a lot of blah blah blah in this so-called file')
tp.flush()
open(tp.name, 'r')
On Windows, you need to close the temporary file before it can be opened:
tp = tempfile.NamedTemporaryFile(delete=False)
tp.write(b'there is a lot of blah blah blah in this so-called file')
tp.close()
open(tp.name, 'r')
You then become responsible for deleting the file once you're done using it.
回答2:
StringIO returns an StringIO object, it's almost equivalent to the file object returned by the open statement. So basically, you can use the StringIO in place of the open statement.
# from io import StringIO for python 3
from StringIO import StringIO
with StringIO('there is a lot of blah blah in this so-called file') as f:
print(f.read())
Output:
there is a lot of blah blah in this so-called file
回答3:
With what I can tell from your comments and recent edits, you want a file that can be opened using the open statement. (I'll leave my other answer be since it's the more correct approach to this type of question)
You can use tempfile to solve your problem, it basically is doing this: create your file, do stuff to your file, then delete your file upon closure.
import os
from tempfile import NamedTemporaryFile
f = NamedTemporaryFile(mode='w+', delete=False)
f.write("there is a lot of blah blah in this so-called file")
f.close()
with open(f.name, "r") as new_f:
print(new_f.read())
os.unlink(f.name) # delete the file after
回答4:
the other answers didn't work for me, but I managed to figure it out.
When using Python 3, you'll want to use io package.
import io
with io.StringIO("some initial text data") as f:
# now you can do things with f as if it was an opened file.
function_that_requires_a_Fileobject_as_argument(f)
来源:https://stackoverflow.com/questions/44381249/treat-a-string-as-a-file-in-python