Any existing component or code?
If you don’t provide a path, the output will be the path of the GH file if it has been saved Relative Path - MetaHopper - Component for Grasshopper | Grasshopper Docs
for the rhino filepath you can use
from Rhino import RhinoDoc a = RhinoDoc.ActiveDoc.Path
in a python node. This will return null if the file has not been saved yet
For Grasshopper, @RIL posted a really simple solution using the C# component:
A = GrasshopperDocument.FilePath;
Hope this is of use.