Neuroimaging in Python — NiBabel 5.4.0.dev1+g3b1c7b37 documentation (original) (raw)
tmpdirs
¶
Contexts for with statement providing temporary directories
TemporaryDirectory([suffix, prefix, dir]) | Create and return a temporary directory. |
---|---|
InGivenDirectory([path]) | Change directory to given directory for duration of with block |
InTemporaryDirectory() | Create, return, and change directory to a temporary directory |
TemporaryDirectory¶
class nibabel.tmpdirs.TemporaryDirectory(suffix='', prefix='tmp', dir=None)¶
Bases: TemporaryDirectory
Create and return a temporary directory. This has the same behavior as mkdtemp but can be used as a context manager.
Upon exiting the context, the directory and everything contained in it are removed.
Please use the standard library tempfile.TemporaryDirectory
- deprecated from version: 5.0
- Will raise <class ‘nibabel.deprecator.ExpiredDeprecationError’> as of version: 7.0
Examples
import os with TemporaryDirectory() as tmpdir: ... fname = os.path.join(tmpdir, 'example_file.txt') ... with open(fname, 'wt') as fobj: ... _ = fobj.write('a string\n') os.path.exists(tmpdir) False
__init__(suffix='', prefix='tmp', dir=None)¶
Please use the standard library tempfile.TemporaryDirectory
- deprecated from version: 5.0
- Will raise <class ‘nibabel.deprecator.ExpiredDeprecationError’> as of version: 7.0
Examples
import os with TemporaryDirectory() as tmpdir: ... fname = os.path.join(tmpdir, 'example_file.txt') ... with open(fname, 'wt') as fobj: ... _ = fobj.write('a string\n') os.path.exists(tmpdir) False
InGivenDirectory¶
nibabel.tmpdirs.InGivenDirectory(path=None)¶
Change directory to given directory for duration of with
block
Useful when you want to use InTemporaryDirectory for the final test, but you are still debugging. For example, you may want to do this in the end:
with InTemporaryDirectory() as tmpdir: ... # do something complicated which might break ... pass
But indeed the complicated thing does break, and meanwhile theInTemporaryDirectory
context manager wiped out the directory with the temporary files that you wanted for debugging. So, while debugging, you replace with something like:
with InGivenDirectory() as tmpdir: # Use working directory by default ... # do something complicated which might break ... pass
You can then look at the temporary file outputs to debug what is happening, fix, and finally replace InGivenDirectory
with InTemporaryDirectory
again.
Parameters:
pathNone or str, optional
path to change directory to, for duration of with
block. Defaults to os.getcwd()
if None
InTemporaryDirectory¶
nibabel.tmpdirs.InTemporaryDirectory()¶
Create, return, and change directory to a temporary directory
Notes
As its name suggests, the class temporarily changes the working directory of the Python process, and this is not thread-safe. We suggest using it only for tests.
Examples
import os from pathlib import Path my_cwd = os.getcwd() with InTemporaryDirectory() as tmpdir: ... _ = Path('test.txt').write_text('some text') ... assert os.path.isfile('test.txt') ... assert os.path.isfile(os.path.join(tmpdir, 'test.txt')) os.path.exists(tmpdir) False os.getcwd() == my_cwd True