Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Treat a string as a file in python

Tags:

python

string

io

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

  1. 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.

  2. 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')

  3. 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'
like image 761
torrho Avatar asked Jun 06 '17 04:06

torrho


People also ask

How do I convert a string to a file type in Python?

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.

How do I convert a string to a CSV file?

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.


2 Answers

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
like image 115
Taku Avatar answered Sep 22 '22 13:09

Taku


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.

like image 35
user2313067 Avatar answered Sep 19 '22 13:09

user2313067