pandas/_testing/contexts.py
def ensure_clean(filename=None, return_filelike=False, **kwargs):
    """
    Gets a temporary path and agrees to remove on close.

    Parameters
    ----------
    filename : str (optional)
        if None, creates a temporary file which is then removed when out of
        scope. if passed, creates temporary file with filename as ending.
    return_filelike : bool (default False)
        if True, returns a file-like which is *always* cleaned. Necessary for
        savefig and other functions which want to append extensions.
    **kwargs
        Additional keywords passed in for creating a temporary file.
        :meth:`tempFile.TemporaryFile` is used when `return_filelike` is ``True``.
        :meth:`tempfile.mkstemp` is used when `return_filelike` is ``False``.
        Note that the `filename` parameter will be passed in as the `suffix`
        argument to either function.

    See Also
    --------
    tempfile.TemporaryFile
    tempfile.mkstemp
    """
    filename = filename or ""
    fd = None

    kwargs["suffix"] = filename

    if return_filelike:
        f = tempfile.TemporaryFile(**kwargs)

        try:
            yield f
        finally:
            f.close()
    else:
        # Don't generate tempfile if using a path with directory specified.
        if len(os.path.dirname(filename)):
            raise ValueError("Can't pass a qualified name to ensure_clean()")

        try:
            fd, filename = tempfile.mkstemp(**kwargs)
        except UnicodeEncodeError:
            import pytest

            pytest.skip("no unicode file names on this system")

        try:
            yield filename
        finally:
            try:
                os.close(fd)
            except OSError:
                print(f"Couldn't close file descriptor: {fd} (file: {filename})")
            try:
                if os.path.exists(filename):
                    os.remove(filename)
            except OSError as e:
                print(f"Exception on removing file: {e}")

    Gets a temporary path and agrees to remove on close.

    Parameters
    ----------
    filename : str (optional)
        if None, creates a temporary file which is then removed when out of
        scope. if passed, creates temporary file with filename as ending.
    return_filelike : bool (default False)
        if True, returns a file-like which is *always* cleaned. Necessary for
        savefig and other functions which want to append extensions.
    **kwargs
        Additional keywords passed in for creating a temporary file.
        :meth:`tempFile.TemporaryFile` is used when `return_filelike` is ``True``.
        :meth:`tempfile.mkstemp` is used when `return_filelike` is ``False``.
        Note that the `filename` parameter will be passed in as the `suffix`
        argument to either function.

    See Also
    --------
    tempfile.TemporaryFile
    tempfile.mkstemp
    
Gets a temporary path and agrees to remove on close.
Parameters

filename : str (optional)
if None, creates a temporary file which is then removed when out of
scope. if passed, creates temporary file with filename as ending.
return_filelike : bool (default False)
if True, returns a file-like which is *always* cleaned. Necessary for
savefig and other functions which want to append extensions.
kwargs
Additional keywords passed in for creating a temporary file.

See Also


Gets a temporary path and agrees to remove on close . Parameters filename : str ( optional ) if None creates a temporary file which is then removed when out of scope . if passed creates temporary file with filename as ending . return_filelike : bool ( default False ) if True returns a file - like which is * always * cleaned . Necessary for savefig and other functions which want to append extensions . kwargs Additional keywords passed in for creating a temporary file . See Also
def ensure_clean(filename=None, return_filelike=False, **kwargs):
    filename = filename or ""
    fd = None
    kwargs["suffix"] = filename
    if return_filelike:
        f = tempfile.TemporaryFile(**kwargs)
        try:
            yield f
        finally:
            f.close()
    else:
        if len(os.path.dirname(filename)):
            raise ValueError("Can't pass a qualified name to ensure_clean()")
        try:
            fd, filename = tempfile.mkstemp(**kwargs)
        except UnicodeEncodeError:
            import pytest
            pytest.skip("no unicode file names on this system")
        try:
            yield filename
        finally:
            try:
                os.close(fd)
            except OSError:
                print(f"Couldn't close file descriptor: {fd} (file: {filename})")
            try:
                if os.path.exists(filename):
                    os.remove(filename)
            except OSError as e:
                print(f"Exception on removing file: {e}")
def ensure_clean ( filename = None , return_filelike = False , ** kwargs ) : filename = filename or "" fd = None kwargs [ "suffix" ] = filename if return_filelike : f = tempfile . TemporaryFile ( ** kwargs ) try : yield f finally : f . close ( ) else : if len ( os . path . dirname ( filename ) ) : raise ValueError ( "Can't pass a qualified name to ensure_clean()" ) try : fd , filename = tempfile . mkstemp ( ** kwargs ) except UnicodeEncodeError : import pytest pytest . skip ( "no unicode file names on this system" ) try : yield filename finally : try : os . close ( fd ) except OSError : print ( f"Couldn't close file descriptor: {fd} (file: {filename})" ) try : if os . path . exists ( filename ) : os . remove ( filename ) except OSError as e : print ( f"Exception on removing file: {e}" )
Gets a temporary path and agrees to remove on close.
Gets a temporary path and agrees to remove on close .
"""
    Gets a temporary path and agrees to remove on close.

    Parameters
    ----------
    filename : str (optional)
        if None, creates a temporary file which is then removed when out of
        scope. if passed, creates temporary file with filename as ending.
    return_filelike : bool (default False)
        if True, returns a file-like which is *always* cleaned. Necessary for
        savefig and other functions which want to append extensions.
    **kwargs
        Additional keywords passed in for creating a temporary file.
        :meth:`tempFile.TemporaryFile` is used when `return_filelike` is ``True``.
        :meth:`tempfile.mkstemp` is used when `return_filelike` is ``False``.
        Note that the `filename` parameter will be passed in as the `suffix`
        argument to either function.

    See Also
    --------
    tempfile.TemporaryFile
    tempfile.mkstemp
    """ # Don't generate tempfile if using a path with directory specified.
filename
return_filelike