How to do virtual file processing?

后端 未结 5 764
滥情空心
滥情空心 2020-11-27 14:15

So for creating files I use the following:

fileHandle = open(\'fileName\', \'w\')

then write the contents to the file, close the file. In t

5条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-27 14:38

    If you mean writing to memory instead of a file, you can simply write the text to a buffer and use the following function:

    def write(text):
      global buffer
      buffer += text + '\n'  # Add a linefeed as you would if you were writing to a file
    
    buffer = ""  # Initialize the buffer
    write("My name is Steve Grafton")
    

    At the end, you will have a buffer that will be the same as if you had written your stuff to a file and then open the file and read all its contents to a buffer! Moreover, you can use the buffer during the process (before having finished your writing) and do searches in it, as if you had created a file for both reading and writing, only that in this case your pointer will

提交回复
热议问题