Python get file type magic




















Bakuriu: file uses libmagic , and that is cross-platform. See the python-magic library John Zwinck links to below. Add a comment. Active Oldest Votes. Improve this answer. Ignacio Vazquez-Abrams Ignacio Vazquez-Abrams k gold badges silver badges bronze badges. Amyth: It does have "guess" in the name, so a bit of leeway is expected. Hmm, get it, but I guess, return '. Amyth: the reason it returns. If you use mimetypes. It seems like mimetypes. There are also libraries for more specialized file types.

For example, the Python standard library has the imghdr module that does the same thing just for image file types. If you need dependency-free pure Python file type checking, see filetype.

The Python Magic library provides the functionality you need. On unix and linux there is the file command to guess file types. There's even a windows port. From the man page :.

File tests each argument in an attempt to classify it. There are three sets of tests, performed in this order: filesystem tests, magic number tests, and language tests. The first test that succeeds causes the file type to be printed. You would need to run the file command with the subprocess module and then parse the results to figure out an extension.

Use Chris Johnson's answer instead. Python 2 imghdr doc Python 3 imghdr doc. You can also install the official file binding for Python, a library called file-magic it does not use ctypes, like python-magic.

It's available on PyPI as file-magic and on Debian as python-magic. For me this library is the best to use since it's available on PyPI and on Debian and probably other distributions , making the process of deploying your software easier.

I've blogged about how to use it , also. As Steven pointed out, subprocess is the way. You can get the command output by the way above as this post said. Stack Overflow for Teams — Collaborate and share knowledge with a private group. Create a free Team What is Teams? Collectives on Stack Overflow.

Oct 8, Sep 27, Sep 17, Dec 29, Nov 22, Apr 2, Mar 31, Download the file for your platform. If you're not sure which to choose, learn more about installing packages. Warning Some features may not work without JavaScript.

Please try enabling it if you encounter problems. Search PyPI Search. Latest version Released: Jun 3, File type identification using libmagic. Navigation Project description Release history Download files. Project links Homepage. Because the size may be different each time you access it depending on what files have been added or removed in the meantime. It recomputes the size of the directory dynamically using the os module —each time you call it at runtime.

You can see how this results in different attribute values at different times in the example from the docs :. Now, you have an initial intuition. Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.

To become more successful in coding, solve more real problems for real people.



0コメント

  • 1000 / 1000