Creating folder / adding file

Hello,

I am trying to launch an app that requires users to create an account (which triggers a personal folder being created) and then upload a small .rtf file (about 20kb) to their folder (for the app to read from). While running locally, this works flawlessly, but when trying to create a new folder or even just upload a file, I am getting errors.

The error for creating a folder:

Traceback (most recent call last):
  File "/opt/render/project/src/.venv/lib/python3.10/site-packages/flask/app.py", line 2190, in wsgi_app
    response = self.full_dispatch_request()
  File "/opt/render/project/src/.venv/lib/python3.10/site-packages/flask/app.py", line 1486, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/opt/render/project/src/.venv/lib/python3.10/site-packages/flask/app.py", line 1484, in full_dispatch_request
    rv = self.dispatch_request()
  File "/opt/render/project/src/.venv/lib/python3.10/site-packages/flask/app.py", line 1469, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
  File "/opt/render/project/src/main.py", line 94, in register
    os.mkdir(path)
FileNotFoundError: [Errno 2] No such file or directory: './uploads/folder'

The error for uploading a file:

Traceback (most recent call last):
  File "/opt/render/project/src/.venv/lib/python3.10/site-packages/flask/app.py", line 2190, in wsgi_app
    response = self.full_dispatch_request()
  File "/opt/render/project/src/.venv/lib/python3.10/site-packages/flask/app.py", line 1486, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/opt/render/project/src/.venv/lib/python3.10/site-packages/flask/app.py", line 1484, in full_dispatch_request
    rv = self.dispatch_request()
  File "/opt/render/project/src/.venv/lib/python3.10/site-packages/flask/app.py", line 1469, in dispatch_request
    return self.ensure_sync(self.view_functions[rule.endpoint])(**view_args)
  File "/opt/render/project/src/main.py", line 60, in home
    form.file.data.save(f'./uploads/{current_user.name}/' + filename)
  File "/opt/render/project/src/.venv/lib/python3.10/site-packages/werkzeug/datastructures/file_storage.py", line 121, in save
    dst = open(dst, "wb")
FileNotFoundError: [Errno 2] No such file or directory: './uploads/alinaaaaaaaa/Untitled.rtf'

This is the code used when creating a new folder (triggered automatically after a new user registration):

path = './uploads/' + new_user.name
os.mkdir(path)

This is the code used when uploading a new file (to a user’s folder):

    if form.validate_on_submit():
        filename = secure_filename(form.file.data.filename)
        form.file.data.save(f'./uploads/{current_user.name}/' + filename)

These are the last 2 hurdles that I am facing, so hopefully someone can help and the app can be launched. Many thanks in advance!! <3

P.S. I’ve only started learning Python 2 months ago, so treat me like I am a newb, please.

Later edit: on some user accounts, it works flawlessly, on others it doesn’t. Is there a way to check the contents of the “uploads” folder and see if the problem accounts maybe simply didn’t have their “personal” folder created?

Also, can I view the contents of the database attached through the environment and edit it? Would like to wipe it clean…

I realised that the errors are being caused by the deployment of recent commits, overwriting the folder “uploads”… is there any way of keeping the newly created folders inside “uploads” between deployments? Maybe by adding the “uploads” folder to the .gitignore file so it doesn’t overwrite it? Or does that remove the folder altogether from render?

The users’ folder and files are getting deleted because the filesystem on Render services are ephemeral, meaning that after any restart (whether that is a new deploy, a manual restart by you, or a restart due to any Render platform catalyzed need), the local files changed are lost. The application always restarts with the last completed deployment state.

See Persistent Disks | Render Docs

2 Likes

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.