10 February 2017
Snippet shows you how to write to temporary directory. You can either write a single file, or do stuff in a directory. In any case you should remove the files and folders after you have done your business, you don't want to clutter your /tmp folder.
Source code viewer
- import os
- import shutil
- import tempfile
- # Temporary file.
- fd, filename = tempfile.mkstemp()
- # Do something with the temp file, like write into it.
- os.write(fd, your_content)
- # Delete the temporary file after usage.
- # Temporary directory.
- # Create temporary directory for csv files.
- temp_dir = tempfile.mkdtemp()
- # Do your work...
- # Clean up the temporary directory.
- shutil.rmtree(temp_dir)Programming Language: Python