what is the encoding of upload .ogg file to mongodb with python?

553 Views Asked by At

i am writing a script for upload a .ogg file to mongodb through the python. i am using python 3.6 and just studying recently, so I haven't any ideas. please help me.

this is my code:

from pymongo import MongoClient
import gridfs
import os`

db=MongoClient().FypDatabase
fs=gridfs.GridFS(db,collection='video')
fs.put(open('C:\\testStream5.ogg','UTF-8'))`

but it say:

Traceback (most recent call last):
    File "C:\Users\chingyi\Desktop\testUp.py", line 16, in <module>
        fs.put(open('C:\\testStream5.ogg','UTF-8'))
            ValueError: invalid mode: 'UTF-8'`

I had do some research. I knew i must write the encoding inside like: Streaming file data into mongodb gridfs so i write fs.put(fileName,'UTF-8') also, i read this page: encoding it say [ "encoding": encoding used for this file. In Python 2, any unicode that is written to the file will be converted to a str. In Python 3, any str that is written to the file will be converted to bytes.]

1

There are 1 best solutions below

2
On BEST ANSWER

OGG Vorbis files are binary files - they do not have a text character encoding.

You only need to supply the encoding argument to GridFs if your data is a str. Similarly, you only need to pass an encoding to open() if the file is text and is not in the default encoding for your platform.

In your case, as you're dealing with a binary file, you need to set the file mode to binary, which negates the encoding argument. Binary read-only file mode is given as rb.

Therefore, you simply need to do:

fs.put(open('C:\\testStream5.ogg', mode='rb'))