File reading

Discussion of Emacs Lisp
Post Reply
minibuffer
Posts: 5
Joined: Mon Feb 21, 2011 4:55 pm

File reading

Post by minibuffer » Tue Feb 22, 2011 3:23 am

Hello! I'm starting Emacs Lisp, and my question is a basic question about file handling, how is it done in Elisp?

I'm coming from Python, so my problem is about the following:

Code: Select all

>>> f=open("btf001.srt")                                                                                      
>>> lines=f.readlines()   
>>> print lines[9:13]                                                                                        
['3\r\n', '00:01:36,300 --> 00:01:38,200\r\n', '{y:i}Oktober \xe4r inventeringstid.\r\n', '\r\n']             
>>>  
How is this kind of simple file reading done in Elisp? (Open the file, read the lines, print the lines from 9 to 12...)

FAU

Re: File reading

Post by FAU » Tue Feb 22, 2011 12:21 pm

I think you have to slurp in the whole file via insert-file-contents into a buffer.

Check out Xah's excellent Emacs Lisp Tutorials: http://xahlee.org/emacs/elisp.html.

You might want to have a look at this http://xahlee.org/emacs/elisp_process_lines.html. That should answer your question.

findinglisp
Posts: 447
Joined: Sat Jun 28, 2008 7:49 am
Location: Austin, TX
Contact:

Re: File reading

Post by findinglisp » Sat Jun 04, 2011 7:08 am

Yes. In Emacs Lisp, you pretty much need to ignore the idea of files. The procedure is to load files into buffers and then examine the contents of the buffer. When you are done, possibly having modified the buffer, then you write the buffer back to a file.
Cheers, Dave
Slowly but surely the world is finding Lisp. http://www.findinglisp.com/blog/

Post Reply