How do I get the filepath for a class in Python?
You can use the inspect module, like this:
import inspect
inspect.getfile(C.__class__)
Find file path of a given class
Use source_location
on its methods:
YourClass.instance_methods(false).map { |m|
YourClass.instance_method(m).source_location.first
}.uniq
You might get more than one location, as methods might be defined in different places.
Getting filesystem path of class being executed
The following code snippet will do this for you:
final File f = new File(MyClass.class.getProtectionDomain().getCodeSource().getLocation().getPath());
replace MyClass
with your main
class
The resulting File
object f
represents the .jar
file that was executed. You can use this object to get the parent to find the directory that the .jar
is in.
How can I return C# class file path
You could use the CallerFilePath attribute:
static void Main(string[] args)
{
Console.WriteLine(GetPath());
Console.Read();
}
static string GetPath([CallerFilePath]string fileName = null)
{
return fileName;
}
How to Find File Path of Class Declaration in a Source Generator
You can use the following code to get the containing file path:
SyntaxNode node = ...;
_ = node.SyntaxTree.FilePath;
_ = node.GetLocation().SourceTree?.FilePath // SourceTree can be null
How to get the path of a running JAR file?
return new File(MyClass.class.getProtectionDomain().getCodeSource().getLocation()
.toURI()).getPath();
Replace "MyClass" with the name of your class.
Obviously, this will do odd things if your class was loaded from a non-file location.
Related Topics
In Ruby, What Is the Cleanest Way of Obtaining the Index of the Largest Value in an Array
How to Convert Array of Activerecord Models to CSV
Where to Place/Access Config File in Gem
How Would You Test Observers with Rspec in a Ruby on Rails Application
How to Call Expire_Fragment from Rails Observer/Model
Does Ruby Have a String.Startswith("Abc") Built in Method
Ruby: How to Concatenate Array of Arrays into One
If String Is Empty Then Return Some Default Value
Find or Create Record Through Factory_Girl Association
Should I Define a Main Method in My Ruby Scripts
How to Convert a JSON Formatted Key Value Pair to Ruby Hash with Symbol as Key
Ruby - Can't Modify Frozen String (Typeerror)
How to List All Versions of a Gem Available at a Remote Site
Method_Missing Gotchas in Ruby
Having Trouble Installing Any Ruby 1.9.X (With Rbenv) on MAC Osx Due to Psych Yaml Parse Errors
Measure and Benchmark Time for Ruby Methods
What Is the Expected Syntax for Checking Exception Messages in Minitest's Assert_Raises/Must_Raise