0

I am looking for something equivalent to this in Python

f = open('2021-11-10.org')
print(f.readline())

I know you can insert the file-contents into a buffer and do something like this, but that is not what I am interested in. I want a way to read a file one line at a time, without reading the whole file in at once.

I want to try making a generator like this (pseudocode):

(iter-defun open (fname)
  (let ((f (open fname)))
    (while (not (eof f))
      (iter-yield (read-line f)))
    (close f)))

Is there anything like this?

4
  • Ah - pythonic elisp :-) I don't know of anything like this. This suggests that it is not easy. But maybe you can do the file/opening/reading/line-yielding with an external python program and just write the consumer in elisp.
    – NickD
    Commented Nov 10, 2021 at 15:27
  • 1
    The generator is easy to define, and a feature in elisp now (gnu.org/software/emacs/manual/html_node/elisp/Generators.html). It is the low level file stuff that seems to be missing. I thought of writing a module for this, but it seems a little too extra just for showing a generator! Commented Nov 10, 2021 at 15:38
  • Oh nice! TIL ...
    – NickD
    Commented Nov 10, 2021 at 16:06
  • Could you not just use a shell-script that does read from stdin, send it a character from emacs, and then have it read from your file (rinse and repeat)? And then just use make-process.
    – rpluim
    Commented Nov 10, 2021 at 17:04

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.