Yes, normpath uses backslashes on Windows.  But it only returns something 
for a file, not for a node that is an *@path* node and not a file.  For 
opening a directory, that's not quite what we want, and we still need the 
os.getcwd() to get the non-file directory paths anyway.  So we might as 
well just stick with getcwd() (or ".", which is the same thing).

Here is the simplest code I have been able to devise that does exactly what 
I want - getting the directory of a node -  if you want to avoid using "." 

import os
nodepath = os.path.normpath(g.fullPath(c, p))
path = os.path.abspath(nodepath)
# can verify with g.es(path)

*g.fullPath(c, p)* returns nothing if the argument is a directory. Calling 
*os.path.normpath* on that empty string produces "."  You can use 
*abspath()* to turn "." into a full path expression, but it was originally 
"." anyway.

OTOH, if it should happen in the future that the when a node is selected 
the current directory no longer gets set to its path, the above path 
expression should still work.  It would also work for some deviant OS that 
doesn't use "." for the working directory.

If you want to get the file path if the node is an external file node, or 
the directory path if it's not, this will do the job:

os.path.abspath(g.fullPath(c, p))


On Wednesday, April 7, 2021 at 10:58:24 AM UTC-4 Edward K. Ream wrote:

> On Tuesday, April 6, 2021 at 9:51:34 PM UTC-5 [email protected] wrote:
>
>> Maybe this is beating the a dead horse, but os.getcwd() returns the 
>> actual directory of the selected node - verified by test.  It is as if when 
>> a node is selected, Leo changes the current directory to the path for that 
>> node. That would explain why "." works as well.
>>
>
> Here is my latest suggestion, incorporating your suggestions. It's only 
> advantage, apparently, is that it is more explicit:
>
> import os
> from subprocess import run
> path = os.path.normpath(g.fullPath(c, p) or os.getcwd())
> cmd = ['explorer.exe', path]
> run(cmd)
>
> Tested only on Windows.  The call to normpath should allow it to work 
> everywhere.
>
> Edward
>

-- 
You received this message because you are subscribed to the Google Groups 
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/leo-editor/f1c24db6-b584-477d-abcc-979b26d49bf6n%40googlegroups.com.

Reply via email to