Skip to content

exodusii.exo_file() does not accept pathlib.Path objects #1

@sswan

Description

@sswan

Upon trying to send in pathlib.Path objects to exodusii.exo_file() I end up getting an error in glob.glob() that kind of says what the problem is but isn't very user friendly to newer python developers.

I'd like to get full support for pathlib.Path objects or a strong type check on the files with a well-written error message. It seems that it is possible to put stars and question marks in pathlib.Path objects, so maybe all we need to do is cast pathlib.Path to str if we encounter any?

>>> p = pathlib.Path("foo/bar.?.*")
>>> p
PosixPath('foo/bar.?.*')
>>> p.resolve()
PosixPath('/Users/mswan/foo/bar.?.*')
>>> str(p)
'foo/bar.?.*'

Other than testing the new feature, an implementation would be this:

mswan@cee-pp-ldrd02 $ git diff __init__.py
diff --git a/exodusii/__init__.py b/exodusii/__init__.py
index 19e62be..c696f7f 100644
--- a/exodusii/__init__.py
+++ b/exodusii/__init__.py
@@ -32,9 +32,13 @@ exo_file = File
 
 def _find_files(*files):
     import glob
+    import pathlib
 
     found = []
     for file in files:
+        if isinstance(file, pathlib.Path):
+            found.append(str(file.resolve()))
+            continue
         globbed_files = glob.glob(file)
         if not globbed_files:
             raise FileNotFoundError(file)

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions