3030from .periodic_table import Elements
3131from .unit_converter import convert
3232from . import numpy_extensions as npe
33+ from .utilities import path_string
3334
3435class TextFile (DevBase ):
3536 # interface to mmap files
@@ -39,11 +40,13 @@ def __init__(self,filepath=None):
3940 self .mm = None
4041 self .f = None
4142 if filepath is not None :
43+ filepath = path_string (filepath )
4244 self .open (filepath )
4345 #end if
4446 #end def __init__
4547
4648 def open (self ,filepath ):
49+ filepath = path_string (filepath )
4750 if not os .path .exists (filepath ):
4851 self .error ('cannot open non-existent file: {0}' .format (filepath ))
4952 #end if
@@ -243,7 +246,8 @@ class StandardFile(DevBase):
243246 def __init__ (self ,filepath = None ):
244247 if filepath is None :
245248 None
246- elif isinstance (filepath , (str , Path )):
249+ elif isinstance (filepath , str | bytes | Path ):
250+ filepath = path_string (filepath )
247251 self .read (filepath )
248252 else :
249253 self .error ('unsupported input: {0}' .format (filepath ))
@@ -255,7 +259,9 @@ def read(self,filepath):
255259 if not os .path .exists (filepath ):
256260 self .error ('read failed\n file does not exist: {0}' .format (filepath ))
257261 #end if
258- self .read_text (open (filepath ,'r' ).read ())
262+ with open (filepath , "r" ) as f :
263+ self .read_text (f .read ())
264+
259265 self .check_valid ('read failed' )
260266 #end def read
261267
@@ -264,7 +270,8 @@ def write(self,filepath=None):
264270 self .check_valid ('write failed' )
265271 text = self .write_text ()
266272 if filepath is not None :
267- open (filepath ,'w' ).write (text )
273+ with open (filepath , "w" ) as f :
274+ f .write (text )
268275 #end if
269276 return text
270277 #end def write
0 commit comments