Skip to content

Latest commit

 

History

History
39 lines (26 loc) · 997 Bytes

2fju.md

File metadata and controls

39 lines (26 loc) · 997 Bytes

lang.py.module.tempfile

Built-in module to create temporary files and directories

Synopsis

  from tempfile import TemporaryFile, TemporaryDirectory

Overview

Cookbook

You can use it to create files and directories that will be deleted once the created resource is closed

Create a temporary directory

You can create a new temporary directory in your systems default temporary objects path by using the TemporaryDirectory class

  • You can get the path name of the created directory as a string with the .name attribute
  • You can manually delete the created temp directory using the .cleanup() method

You can use it in a context manager and it will handle clean up automatically. It will pas the contents of the .name attribute

  from tempfile import TemporaryDirectory

  with TemporaryDirectory() as tmpdirname:
      print(f"Created temporary directory {tmpdirname}")

# Created temporary directory /tmp/tvwdfuwlfh