Check whether a PDF-File is valid with Python

后端 未结 7 605
借酒劲吻你
借酒劲吻你 2020-12-08 10:50

I get a File via a HTTP-Upload and need to be sure its a pdf-file. Programing Language is Python, but this should not matter.

I thought of the follow

7条回答
  •  不知归路
    2020-12-08 11:33

    In a project if mine I need to check for the mime type of some uploaded file. I simply use the file command like this:

    from subprocess import Popen, PIPE
    filetype = Popen("/usr/bin/file -b --mime -", shell=True, stdout=PIPE, stdin=PIPE).communicate(file.read(1024))[0].strip()
    

    You of course might want to move the actual command into some configuration file as also command line options vary among operating systems (e.g. mac).

    If you just need to know whether it's a PDF or not and do not need to process it anyway I think the file command is a faster solution than a lib. Doing it by hand is of course also possible but the file command gives you maybe more flexibility if you want to check for different types.

提交回复
热议问题