How to create fake text file in Python

百般思念 提交于 2019-12-03 11:11:46
Dougal

This is exactly what StringIO/cStringIO (renamed to io.StringIO in Python 3) is for.

Joran Beasley

Or you could implement it yourself pretty easily especially since all you need is readlines():

def FileSpoof:
     def __init__(self,my_text):
         self.my_text = my_text
     def readlines(self):
         return self.my_text.splitlines()

then just call it like:

somefake = FileSpoof("This is a bunch\nOf Text!")
print somefake.readlines()

That said the other answer is probably more correct.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!