pymatgen
pymatgen copied to clipboard
Inconsistent use of output file path in Structure.to() method
Describe the bug Structure.to() method ignores the parent path of filename for certain file formats, say, .xsf format. For example, in the following scenario,
struct = Structure(...)
struct.to(filename="/path/to/struct.ext")
the output file is correctly written to /path/to/struct.ext when .ext is .cif, .mcif, .json, .cssr, .poscar. But, for some formats including .xsf, it is written at ./struct.ext (current directory).
This is because, in the source code of to(), fname (which only contains the basename of the given filename) is used instead of filename variable for some extensions. For example, in core/structure.py:2303-2310,
elif fmt == "xsf" or fnmatch(fname.lower(), "*.xsf*"):
from pymatgen.io.xcrysden import XSF
s = XSF(self).to_string()
if filename:
with zopen(fname, "wt", encoding="utf8") as f:
f.write(s)
return
zopen uses fname instead of filename. It is also the case for some other extensions.
Expected behavior I think to() method should always use filename as the output path.
Desktop (please complete the following information):
- OS: MacOS Big Sur