Forums

TypeError: stat() argument 1 must be encoded string without null bytes, not str

I am trying to read contents of files like .txt,.docx,.pdf and so on with textract and flask microframework. I am using POST method to get input file and send it to textract.

@app.route('/upload', methods=['POST'])
    def upload():
        request_file = request.files['file']
        r = request_file.read()
        text = textract.process(r)
        return (text)

when i uploaded a docx file,

File "/usr/lib/python2.7/genericpath.py", line 26, in exists
os.stat(path) TypeError: stat() argument 1 must be encoded string
without null bytes, not str 10.0.2.2 -- [17/Apr/2018 03:56:58] "POST
/upload HTTP/1.1" 500 -

I don't want to save the file. All i need is to send the file into textract and process the file. Any help is much apprecaited.

[edit by admin: formatting]

Could you give the full stack trace, including anything that came before the error you provided?

The line request_file.read() open and reads the contents of the file. I want to send it as a file instead of contents. Here is the full error:

pic

Is there a way i can send my input file to textract instead of contents ??

Hmm, it looks like you're running that code locally and not on PythonAnywhere, so there's a limit to how much help I can give you. But if I'm understanding the textract documentation correctly, the function textract.process requires a path to a real file on disk -- it can't handle the file-like objects that Flask provides for uploaded files. So if you want to use textract, you'll have to save the incoming files to a temporary place on disk, tell textract to process them, then tidy up.