适用于Linux的"Python"“文件"的Python类似物命令 [英] Python analog for linux "file" command

查看:74
本文介绍了适用于Linux的"Python"“文件"的Python类似物命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找用Python制作的file linux命令类似物.它应该提供有关文件类型的信息,如man file中所述.我正在寻找的最小功能集是确定文件是原始文件还是文本(人类可读)文件.包装库将是一个很好的建议. 我知道,我可以将file作为子进程运行,并获取其输出以确定文件类型.但是我的程序应该解析成千上万个文件,在这种情况下,我担心执行时间会很长.

I'm looking for file linux command analog made in Python. It should provide information about file type as described in man file. Minimal feature set I'm looking for is to determine if file is raw or text (human-readable) one. Wrapper library will be good suggestion. I know, I can run file as subprocess and grab it's output to determine file type. But my program is supposed to parse thousands of files and I'm afraid of very long execution time in this case.

推荐答案

,您需要检查文件的魔术"字节,我正要告诉您:

you need to check the "magic" byte of the file, and I was about to tell you about:

当我想到应该已经在SO上回答了这个问题,并且

when it occured to me that this question should already have been answered on SO, and it has.

N.B .:我没有像其他帖子一样列出pymagic,因为0.1看起来很旧(即使源网站关闭),也没有得到任何更新.

N.B.: I'm not listing pymagic as the other post does, as it did not get any update since 0.1 which looks quite old (even the source website is down).

对于OSX:

brew install libmagic
pip install python-magic
python
>>> magic.from_file('test.py')
'Python script, ASCII text executable'

这篇关于适用于Linux的"Python"“文件"的Python类似物命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆