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')
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')
open
with no success.Any help is appreciated! :-)
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'
Python – Write String to Text File To write string to a Text File, follow these sequence of steps: Open file in write mode using open() function. Write string to the file using write() method. Close the file using close() method.
Steps for writing a CSV file First, open the CSV file for writing ( w mode) by using the open() function. Second, create a CSV writer object by calling the writer() function of the csv module. Third, write data to CSV file by calling the writerow() or writerows() method of the CSV writer object.
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 StringIO import StringIO # for Python 2, old
from io 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
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With