Search code examples
pythonpersistencetemporary-files

How to make Python temporary files and folders persistent for a defined period of days?


I want to use tempfile module, to generate files and keep them in /tmp/myfolder/ and keep them there until a certain time (maybe in days). I learned that tempfile removes file as soon as f.close() is run. I just don't want it to auto-remove. Will remove later. Is this possible with tempfile? or any other better approach?


Solution

  • You can use this:

    import tempfile
    with tempfile.NamedTemporaryFile(delete=False, dir='/tmp/myfolder') as outfile:
        # ...