Skip to content Skip to sidebar Skip to footer

What Is The Equivalent To Python Equivalent To Using Class.getresource()

In java if I want to read a file that contains resource data for my algorithms how do I do it so the path is correctly referenced. Clarification I am trying to understand how in th

Solution 1:

I think you may be looking for pkgutil.get_data(). The docs for this say:

pkgutil.get_data(package, resource)

Get a resource from a package.

This is a wrapper for the PEP 302 loader get_data() API. The package argument should be the name of a package, in standard module format (foo.bar). The resource argument should be in the form of a relative filename, using / as the path separator. The parent directory name .. is not allowed, and nor is a rooted name (starting with a /).

The function returns a binary string that is the contents of the specified resource.

For packages located in the filesystem, which have already been imported, this is the rough equivalent of:

d = os.path.dirname(sys.modules[package].__file__)
data = open(os.path.join(d, resource), 'rb').read()

If the package cannot be located or loaded, or it uses a PEP 302 loader which does not support get_data(), then None is returned.

Solution 2:

I think you are looking for imp.load_source:

importimpmodule= imp.load_source('ModuleName', '/path/of/the/file.py')
module.FooBar()

Solution 3:

For Pythonistas who don't know, the behaviour of Java's Class.getResource is basically: the supplied file name is (unless it's already an absolute path) transformed into a relative path by using the class' package (since the directory path to the class file is expected to mirror the explicit "package" declaration for the class). The ClassLoader that was used to load the class in the first place then gets to transform this path string, by its own logic, into a URL object that could encode a file name, a location on the WWW, etc.

Python is not Java, so we have to approximate a few things and read intent into the question.

Python classes don't really explicitly go into packages, although you can create packages by putting them in folders with an additional __init__.py file.

Python does not really have anything quite like the URL class in its standard library; although there is plenty of support for connecting to the Internet, you're generally expected to just use strings to represent URLs (and file names) and format them appropriately. This is arguably an unfortunate missed opportunity for polymorphism (it would not be hard to make your own wrapper, though you might miss lots of special cases and useful functionality). Anyway, in normal cases with Java, you're not expecting to get a web URL from this process.

Python has a concept of a "working directory" that depends on how the Python process was launched. File paths are not necessarily relative to the directory where the "main class" (well, really, "main module", because Python doesn't make you put everything in a class) is found.

So what you really want, probably, is to get the absolute path on disk to the source file corresponding to the class. But that isn't really going to work out either. The problem is: given a class, you can get the name of the module it comes from, and then look up that name to get the actual module object, and then from the module object get the file name that the module was loaded from. However, that file name is relative to whatever the working directory was when the module was loaded, and that information isn't recorded. If the working directory has changed since then (with os.chdir), you're out of luck.

Please try to be more clear about what you're really trying to do.

Post a Comment for "What Is The Equivalent To Python Equivalent To Using Class.getresource()"