2

I would like to use some python code with babel to output some computation result about the current org file. As I'll be doing this quite often, I'd like ideally to have access to it's file name without hard-writing it's name in my code.

For instance:

#+begin_src python :results output :exports results
with open("myself.org") as f:
    contents = f.read()
print "Current org file line count: %d" % len(contents.split("\n"))
#+end_src

I would want to get rid of the hard-writen "myself.org", which is supposed to point towards the current file name.

Is there any macro possibility in source code block that would insert the current org file's name in the some placeholder in this code block before evaluation ?

Or is there any way to add variables in python code scope to all code blocks which would then allow access to the current file path at run time through variables or any other means ?

  • 1
    Can you give a stub example of what you are trying to do? – stsquad Sep 3 '15 at 17:58
3

Please try a src block variable like: :var fileName=(buffer-file-name) and then you have the current file name accessible in python in the variable named fileName

You might set a property line in the begining of your file #+PROPERTY: header-args:python :var fileName=( buffer-file-name) and you have the variable in ever python src block. Don't forget to initialise the line with C-c C-c. Or reload the buffer into Emacs.

  • Thanks, thats nearly quite it. Do you have a suggestion how I could put this variable by default on all blocks ? Anyway, your answer covers nearly completely my question. I didn't know about using lisp in these header (I'm quite new to org-mode). – vaab Sep 3 '15 at 20:23
  • 1
    @vaab enhanced my answer, please check, I didn't test it. – Dieter.Wilhelm Sep 4 '15 at 4:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.