strings
是一个 GNU/Linux 应用程序,用于打印文件中可打印字符串。
有什么方法可以做 strings
所做的事情,但是在 Python 中吗?
在我的情况下,调用strings
并获取输出不是一个选项。
最佳答案
如果你不关心输出的内容,只要简单地忽略所有解码错误就很容易实现:
在 python2 中:
with open('file') as fd:
print fd.read().decode('ascii', errors='ignore')
在 python3 中:
import codecs
with open('file') as fd:
print(codecs.decode(fd.read(), 'ascii', errors='ignore'))
无论如何,errors='ignore'
都会忽略解码过程中的所有错误。
关于Python:检测二进制文件中的所有字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31947756/