tmpdirs
¶
Contexts for with statement providing temporary directories
|
Change directory to given directory for duration of |
|
Create, return, and change directory to a temporary directory |
|
Create and return a temporary directory. |
InGivenDirectory
¶
-
class
nibabel.tmpdirs.
InGivenDirectory
(path=None)¶ Bases:
object
Change directory to given directory for duration of
with
blockUseful 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 the
InTemporaryDirectory
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
withInTemporaryDirectory
again.Initialize directory context manager
- Parameters
- pathNone or str, optional
path to change directory to, for duration of
with
block. Defaults toos.getcwd()
if None
-
__init__
(path=None)¶ Initialize directory context manager
- Parameters
- pathNone or str, optional
path to change directory to, for duration of
with
block. Defaults toos.getcwd()
if None
InTemporaryDirectory
¶
-
class
nibabel.tmpdirs.
InTemporaryDirectory
(suffix='', prefix='tmp', dir=None)¶ Bases:
nibabel.tmpdirs.TemporaryDirectory
Create, return, and change directory to a temporary directory
Examples
>>> import os >>> my_cwd = os.getcwd() >>> with InTemporaryDirectory() as tmpdir: ... _ = open('test.txt', 'wt').write('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
-
__init__
(suffix='', prefix='tmp', dir=None)¶ Initialize self. See help(type(self)) for accurate signature.
-
TemporaryDirectory
¶
-
class
nibabel.tmpdirs.
TemporaryDirectory
(suffix='', prefix='tmp', dir=None)¶ Bases:
object
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 everthing contained in it are removed.
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)¶ Initialize self. See help(type(self)) for accurate signature.
-
cleanup
()¶
-