Recently, I have discovered the "tempfile" module, which enables you to easily create temporary files used by your Python program.
However, while trying to use the module, I have discovered an issue - Most Python modules that manipulate or create files, use the path to the file (as a string) as inputs from the user (or a programmer).
That means, that code like this one will raise an error:
import tempfile
with tempfile.NamedTemporaryFile(suffix=".jpg") as tempfileimg:
image = generate_random_image()
save_image(image, tempfileimg) # Type Error (expected path to file)
upload_image_to_cloud(tempfileimg) # Type Error (expected path to file)
# In this example, both `save_image` and `upload_image_to_cloud` functions
# expect to receive the path to the file and not the "opened file object".
I have tried using tempfileimg.name
to get the path to the temporary file as a string, but because temporary files are located in a special folder on the machine, writing files in the regular methods will raise an expectation:
PermissionError: [Errno 13] Permission denied: 'C:\\Users\\MyUserName\\AppData\\Local\\Temp\\tmpvq81yj2v.jpg'
How can I solve this issue?