Another way of working with temporary files

đź—‚ tmp-folder allows you to easily use a temporary folder during the execution of a function. After it is done executing, the folder will be automatically deleted (with all its content of course).

Install it

pip install tmp-folder


This content originally appeared on DEV Community and was authored by Jorge Alvarado

đź—‚ tmp-folder allows you to easily use a temporary folder during the execution of a function. After it is done executing, the folder will be automatically deleted (with all its content of course).

Install it

pip install tmp-folder

Watch it in action

All you need to do is to decorate the function that needs a temporary folder:

# main.py
from pathlib import Path

from tmp_folder import use_tmp_folder


@use_tmp_folder  # This is the decorator
def do_some_stuff(my_tmp_folder: Path) -> Path:
    with open(my_tmp_folder / "some_file.txt", "w") as file:
        file.write("Hello World")

    assert my_tmp_folder.exists()

    return my_tmp_folder


if __name__ == "__main__":
    my_tmp_folder = do_some_stuff()
    assert not my_tmp_folder.exists()
    print("Everything went ok")

We execute the code with python main.py:

Everything went ok

✅ Cool, there are no assertion errors 👏🎉🎉🎉.

Key points:

  • The function that needs temporary stuff, is decorated with use_tmp_folder.
  • You get a new parameter in your decorated function (it can be named however you want, in this case my_tmp_folder). See how when you are calling do_some_stuff, you are not passing any argument, but on its definition there is a parameter, well, that's because tmp-folder adds it for you.
  • This new parameter is the temporary folder that tmp-folder gives you. You can store whatever you want in there. After the decorated function is done executing: bye bye temporary folder đź‘‹.
  • The parameter is a Path object, so you get all the cool things pathlib offers.
  • The decorator is built on top of Python's TemporaryDirectory, so you know is a safe thing to use.

For more details see the package documentation.
Image author.


This content originally appeared on DEV Community and was authored by Jorge Alvarado


Print Share Comment Cite Upload Translate Updates
APA

Jorge Alvarado | Sciencx (2022-02-03T13:32:30+00:00) Another way of working with temporary files. Retrieved from https://www.scien.cx/2022/02/03/another-way-of-working-with-temporary-files/

MLA
" » Another way of working with temporary files." Jorge Alvarado | Sciencx - Thursday February 3, 2022, https://www.scien.cx/2022/02/03/another-way-of-working-with-temporary-files/
HARVARD
Jorge Alvarado | Sciencx Thursday February 3, 2022 » Another way of working with temporary files., viewed ,<https://www.scien.cx/2022/02/03/another-way-of-working-with-temporary-files/>
VANCOUVER
Jorge Alvarado | Sciencx - » Another way of working with temporary files. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2022/02/03/another-way-of-working-with-temporary-files/
CHICAGO
" » Another way of working with temporary files." Jorge Alvarado | Sciencx - Accessed . https://www.scien.cx/2022/02/03/another-way-of-working-with-temporary-files/
IEEE
" » Another way of working with temporary files." Jorge Alvarado | Sciencx [Online]. Available: https://www.scien.cx/2022/02/03/another-way-of-working-with-temporary-files/. [Accessed: ]
rf:citation
» Another way of working with temporary files | Jorge Alvarado | Sciencx | https://www.scien.cx/2022/02/03/another-way-of-working-with-temporary-files/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.