# Problem with temporary files

**URL:** <https://discuss.huggingface.co/t/problem-with-temporary-files/23187>\
**Category:** Spaces\
**Created:** [September 15, 2022, 2:45pm UTC](https://discuss.huggingface.co/t/problem-with-temporary-files/23187 "2022-09-15T14:45:12Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![diegofernandezc](https://avatars.discourse-cdn.com/v4/letter/d/c0e974/32.png) [@diegofernandezc](https://discuss.huggingface.co/u/diegofernandezc)\
**Post date:** [September 15, 2022, 2:45pm UTC](https://discuss.huggingface.co/t/problem-with-temporary-files/23187/1 "2022-09-15T14:45:12Z")

</div>

Hi,

I have a problem using the tempfile library on Spaces. This problem occurs sometimes.

I have [this](https://huggingface.co/spaces/tryolabs/norfair-demo) space that generates an output file that is a disk write and the main function returns the path to this file.

I know that if I do not use a tempfile, this works correctly, but I’d like to use tempfile to prevent generations of outputs in a local execution.

[This](https://huggingface.co/spaces/tryolabs/norfair-demo/blob/main/inference.py) is the function that generates the problem (temporary file lines are commented out to avoid errors).

And the logs when the execution fails are the following:

Traceback (most recent call last):  
File “/home/user/.local/lib/python3.8/site-packages/gradio/routes.py”, line 259, in run\_predict  
output = await app.blocks.process\_api(  
File “/home/user/.local/lib/python3.8/site-packages/gradio/blocks.py”, line 691, in process\_api  
predictions = self.postprocess\_data(fn\_index, predictions, state)  
File “/home/user/.local/lib/python3.8/site-packages/gradio/blocks.py”, line 657, in postprocess\_data  
block.postprocess(prediction\_value)  
File “/home/user/.local/lib/python3.8/site-packages/gradio/components.py”, line 1676, in postprocess  
y = processing\_utils.create\_tmp\_copy\_of\_file(y, dir=self.temp\_dir)  
File “/home/user/.local/lib/python3.8/site-packages/gradio/processing\_utils.py”, line 296, in create\_tmp\_copy\_of\_file  
shutil.copy2(file\_path, file\_obj.name)  
File “/usr/local/lib/python3.8/shutil.py”, line 435, in copy2  
copyfile(src, dst, follow\_symlinks=follow\_symlinks)  
File “/usr/local/lib/python3.8/shutil.py”, line 264, in copyfile  
with open(src, ‘rb’) as fsrc, open(dst, ‘wb’) as fdst:  
FileNotFoundError: [Errno 2] No such file or directory: ‘/tmp/tmpsp16mbfp/soccer532nbnksvotwvex0\_out.mp4’

As I mentioned earlier, this code runs perfectly in many situations, but sometimes finish with the above error.

Any suggestions to use the tempfile library capabilities properly on Spaces?

Thank you,  
Diego.

---

<div class="post-metadata">

**Author:** ![freddyaboulton](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/freddyaboulton/32/32732_2.png) [@freddyaboulton](https://discuss.huggingface.co/u/freddyaboulton)\
**Post date:** [September 15, 2022, 6:57pm UTC](https://discuss.huggingface.co/t/problem-with-temporary-files/23187/2 "2022-09-15T18:57:14Z")

</div>

Hi @diegofernandezc !

You may have to copy your file into your temporary directory so that it doesn’t get deleted?

I think it’s fine if your demo doesn’t create a temporary file. Gradio will do that under the hood for you when your output component is `File`.

---

<div class="post-metadata">

**Author:** ![HandaryDesuja](https://sea2.discourse-cdn.com/hellohellohello/user_avatar/discuss.huggingface.co/handarydesuja/32/22476_2.png) [@HandaryDesuja](https://discuss.huggingface.co/u/HandaryDesuja)\
**Post date:** [December 27, 2023, 7:13am UTC](https://discuss.huggingface.co/t/problem-with-temporary-files/23187/3 "2023-12-27T07:13:50Z")

</div>

I looked for this on various websites including youtube and solved it using to verify the temp folder [path](https://thegeekpage.com/error-writing-a-temporary-file/).
