Temporary Filesystem

Manage filesystems in temporary locations.

A temporary filesytem is stored in a location defined by your OS (/tmp on linux). The contents are deleted when the filesystem is closed.

A TempFS is a good way of preparing a directory structure in advance, that you can later copy. It can also be used as a temporary data store.

class fs.tempfs.TempFS(identifier='__tempfs__', temp_dir=None, auto_clean=True, ignore_clean_errors=True)[source]

A temporary filesystem on the OS.

  • identifier (str) – A string to distinguish the directory within the OS temp location, used as part of the directory name.
  • temp_dir (str, optional) – An OS path to your temp directory (leave as None to auto-detect)
  • auto_clean (bool) – If True (the default), the directory contents will be wiped on close.
  • ignore_clean_errors (bool) – If True (the default), any errors in the clean process will be suppressed. If False, they will be raised.

Clean (delete) temporary files created by this filesystem.


Close the filesystem and release any resources.

It is important to call this method when you have finished working with the filesystem. Some filesystems may not finalize changes until they are closed (archives for example). You may call this method explicitly (it is safe to call close multiple times), or you can use the filesystem as a context manager to automatically close.


>>> with OSFS('~/Desktop') as desktop_fs:
...    desktop_fs.writetext(
...        'note.txt',
...        "Don't forget to tape Game of Thrones"
...    )

If you attempt to use a filesystem that has been closed, a FilesystemClosed exception will be thrown.