Skip to content

FileStorage.save() does not support pathlib #1653

@manlix

Description

@manlix

Description

I expected save uploaded file to pathlib object.

Code

from pathlib import Path

# ...

def path_to_save(document_id):
    return Path(myproject.config.DocumentProcessing.save_to(document_id))

# ...

request.files['document'].save(path_to_save(document_id))

# ...

Actual Behavior

Uploaded file document did not save to filesystem:

Traceback (most recent call last):
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1949, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python3.7/site-packages/flask/app.py", line 1935, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/var/lib/myproject/myproject/website/blueprints/document.py", line 23, in create_document
    result = schemas.DocumentSchema().dump(utils.uploaded_document())
  File "/var/lib/myproject/myproject/website/utils.py", line 39, in uploaded_document
    request.files['document'].save(path_to_save(document_id)) 
  File "/usr/local/lib/python3.7/site-packages/werkzeug/datastructures.py", line 2803, in save
    copyfileobj(self.stream, dst, buffer_size)
  File "/usr/local/lib/python3.7/shutil.py", line 82, in copyfileobj
    fdst.write(buf)
AttributeError: 'PosixPath' object has no attribute 'write'

Expected Behavior

Uploaded file document saved to filesystem.

Versions

Python: 3.7.4
Werkzeug: 0.16.0

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions