@@ -367,13 +367,36 @@ def is_numlike(obj):
367367 return isinstance (obj , (numbers .Number , np .number ))
368368
369369
370- def to_filehandle (fname , flag = 'rU ' , return_opened = False , encoding = None ):
370+ def to_filehandle (fname , flag = 'r ' , return_opened = False , encoding = None ):
371371 """
372- *fname* can be an `os.PathLike` or a file handle. Support for gzipped
373- files is automatic, if the filename ends in .gz. *flag* is a
374- read/write flag for :func:`file`
372+ Convert a path to an open file handle or pass-through a file-like object.
373+
374+ Consider using `open_file_cm` instead, as it allows one to properly close
375+ newly created file objects more easily.
376+
377+ Parameters
378+ ----------
379+ fname : str or PathLike or file-like object
380+ If `str` or `os.PathLike`, the file is opened using the flags specified
381+ by *flag* and *encoding*. If a file-like object, it is passed through.
382+ flag : str, default 'r'
383+ Passed as the *mode* argument to `open` when *fname* is `str` or
384+ `os.PathLike`; ignored if *fname* is file-like.
385+ return_opened : bool, default False
386+ If True, return both the file object and a boolean indicating whether
387+ this was a new file (that the caller needs to close). If False, return
388+ only the new file.
389+ encoding : str or None, default None
390+ Passed as the *mode* argument to `open` when *fname* is `str` or
391+ `os.PathLike`; ignored if *fname* is file-like.
392+
393+ Returns
394+ -------
395+ fh : file-like
396+ opened : bool
397+ *opened* is only returned if *return_opened* is True.
375398 """
376- if isinstance (fname , getattr ( os , " PathLike" , ()) ):
399+ if isinstance (fname , os . PathLike ):
377400 fname = os .fspath (fname )
378401 if isinstance (fname , str ):
379402 if fname .endswith ('.gz' ):
0 commit comments