get path of running script?

42 views
Skip to first unread message

Matt Wilkie

unread,
Oct 14, 2019, 10:50:42 PM10/14/19
to leo-editor
What's Leo's idiomatic way to get the path of the currently running script in Leo? I've been using the below, but it's proven unreliable in Leo (differs between `Ctrl-B` and `--script=xx`), and there's probably already a Leo function which does this anyway, right?

def get_my_path():
   
"""Return full path of currently running script or executable    
   Thanks to
        https://stackoverflow.com/questions/2632199/
            how-do-i-get-the-path-of-the-current-executed-file-in-python
        @jose-crespo-barrios, https://stackoverflow.com/a/44592299/14420
    """

   
import inspect

    filename
= inspect.getframeinfo(inspect.currentframe()).filename
   
return filename

-matt

Matt Wilkie

unread,
Oct 15, 2019, 12:12:30 AM10/15/19
to leo-editor
c.writeScriptFile seems to be where it is created. I haven't found a place yet for retrieving it; maybe it's too shortlived to bother with.

-matt

Edward K. Ream

unread,
Oct 15, 2019, 4:47:58 AM10/15/19
to leo-editor
On Mon, Oct 14, 2019 at 9:50 PM Matt Wilkie <map...@gmail.com> wrote:
What's Leo's idiomatic way to get the path of the currently running script in Leo?

Not sure I know what you mean by the "currently running script".  There are several ways of getting important directories.

g.app.loadDir is the leo/core directory.

c.fileName() is the .leo file itself, so you can use os.path.dirname(c.fileName()) to get the directory containing the .leo file.

From inside a file, you can use os.path.dirname(__file__).

etc.

Edward

Matt Wilkie

unread,
Oct 16, 2019, 1:34:13 AM10/16/19
to leo-editor
What's Leo's idiomatic way to get the path of the currently running script in Leo?

Not sure I know what you mean by the "currently running script".  There are several ways of getting important directories.

I meant the name of the file that is created and then run when executing Ctrl-B or using @button. It's part of troubleshooting #1376. I have since determined Ctrl-B name is  predictable: always HOME/.leo/scriptFile.py.

(It occurs to me re-using the same name is a problem waiting to happen. Now that multiple Leo sessions can run concurrently so long as they're not editing the same .leo file, who's to say which one created the file that's about to be run?)

-matt


 

Edward K. Ream

unread,
Oct 16, 2019, 5:59:52 AM10/16/19
to leo-editor
On Wed, Oct 16, 2019 at 12:34 AM Matt Wilkie <map...@gmail.com> wrote:
What's Leo's idiomatic way to get the path of the currently running script in Leo?

Not sure I know what you mean by the "currently running script".  There are several ways of getting important directories.

I meant the name of the file that is created and then run when executing Ctrl-B or using @button. It's part of troubleshooting #1376. I have since determined Ctrl-B name is  predictable: always HOME/.leo/scriptFile.py.

Glad you figured this out.

(It occurs to me re-using the same name is a problem waiting to happen. Now that multiple Leo sessions can run concurrently so long as they're not editing the same .leo file, who's to say which one created the file that's about to be run?)

Seems like a low priority to me, unless the multiple Leo sessions were opened by humans and not a script.  Feel free to create a new issue if you think this is important.

Edward
Reply all
Reply to author
Forward
0 new messages