diff --git a/src/diffpy/structure/__init__.py b/src/diffpy/structure/__init__.py index f8106490..cdfcb28f 100644 --- a/src/diffpy/structure/__init__.py +++ b/src/diffpy/structure/__init__.py @@ -37,6 +37,8 @@ # Interface definitions ------------------------------------------------------ +import os + from diffpy.structure.atom import Atom from diffpy.structure.lattice import Lattice from diffpy.structure.parsers import getParser @@ -55,8 +57,7 @@ def loadStructure(filename, fmt="auto", **kw): Parameters ---------- - - filename : str + filename : str or pathlib.Path Path to the file to be loaded. fmt : str, Optional Format of the structure file such as 'cif' or 'xyz'. Must be @@ -74,7 +75,7 @@ def loadStructure(filename, fmt="auto", **kw): Return a more specific PDFFitStructure type for 'pdffit' and 'discus' formats. """ - + filename = os.fspath(filename) # This handles str, Path, and os.PathLike p = getParser(fmt, **kw) rv = p.parseFile(filename) return rv diff --git a/tests/test_loadstructure.py b/tests/test_loadstructure.py index 4f099c7d..a98c6031 100644 --- a/tests/test_loadstructure.py +++ b/tests/test_loadstructure.py @@ -59,6 +59,17 @@ def test_badkwarg(self): self.assertRaises(TypeError, loadStructure, f, eps=1e-10) return + def test_cif_pathlib(self): + """check loading CIF file using pathlib.Path input""" + from pathlib import Path + + f = self.datafile("PbTe.cif") + f_path = Path(f) # Convert to Path object + stru = loadStructure(f_path) + self.assertTrue(isinstance(stru, Structure)) + self.assertFalse(isinstance(stru, PDFFitStructure)) + return + # End of class TestLoadStructure