Search code examples
pythontemporary-files

How to save a temporarily jpg file by using python?


Hey I need to save a temporarily jpg file and then remove it, is there any better way to do? I tested the tempfile, but looks that doesn't work.


Solution

  • tempfile does work. What did you try?

    >>> with tempfile.NamedTemporaryFile(mode="wb") as jpg:
    ...     jpg.write(b"Hello World!")
    ...     print jpg.name
    ...
    c:\users\<...>\appdata\local\temp\tmpv7hy__
    

    jpg will be closed as soon as the with block is left. If you pass in the optional argument delete, it will be deleted on close.