[Fixed]-Expected str, bytes or os.PathLike object, not InMemoryUploadedFile

25๐Ÿ‘

โœ…

The error is happening because the function handle_uploaded_file(f) is trying to open an already opened file.

The value of request.FILES['file'] is a InMemoryUploadedFile and can be used like a normal file. You donโ€™t need to open it again.

To fix, just remove the line that tries to open the file:

def handle_uploaded_file(f):
    for x in f:
        if x.startswith('newick;'):
            print('')
    return cutFile(x)
๐Ÿ‘คWill Keeling

1๐Ÿ‘

the solution for me;

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

instead of :

MEDIA_ROOT = [os.path.join(BASE_DIR, 'media')]

in settigs.py

๐Ÿ‘คDeniz

0๐Ÿ‘

In my setting.py I had

MEDIA_ROOT = os.path.join(BASE_DIR, 'media'),

when it shouldโ€™ve been

MEDIA_ROOT = os.path.join(BASE_DIR, 'media')

which solved this error for me.

๐Ÿ‘คRichard E Long

0๐Ÿ‘

Save the InMemoryUploadedFile to a temporary file.

you can save the InMemoryUploadedFile to a temporary file and then read the data from that file.

import tempfile

if input:
    with tempfile.NamedTemporaryFile(delete=False) as temp_file:
        temp_file.write(input.read())
        temp_file.close()

    with open(temp_file.name, 'r') as file:
        image_data = file.read()
๐Ÿ‘คPooja

Leave a comment