Python小程序:获取二进制文件的所有内容
来源:互联网 发布:苹果6在线软件 编辑:程序博客网 时间:2024/05/21 19:31
接上一篇:Python小程序:获取文本文件的所有内容
有时候希望获取一个二进制文件的所有内容,但又不希望有打开文件、读文件、关闭文件这些繁琐的步骤,因此需要用一个小程序把这几个步骤封装起来,一句话完成所需要的获取文件内容的操作。为此,这里给出一个示例代码。
代码如下(get_bin_file.py):
#! /usr/bin/env pythonimport osdef get_bin_file(filename):'''Get the all content of the binary file.input: filename - the binary file namereturn: binary string - the content of the file. '''if not os.path.isfile(filename):print("ERROR: %s is not a valid file." % (filename))return Nonef = open(filename, "rb")data = f.read()f.close()return data
调用示例:
>>> import get_bin_file>>> content = get_bin_file.get_bin_file("not_exist_file")ERROR: not_exist_file is not a valid file.>>> content = get_bin_file.get_bin_file("./get_bin_file.py")>>> print (content)b'#! /usr/bin/env python\n\nimport os\n\ndef get_bin_file(filename):\n\t\'\'\'\n\tGet the all content of the binary file.\n\t\n\tinput: filename - the binary file name\n\treturn: binary string - the content of the file. \n\t\'\'\'\n\n\tif not os.path.isfile(filename):\n\t\tprint("ERROR: %s is not a valid file." % (filename))\n\t\treturn None\n\n\tf = open(filename, "rb")\n\tdata = f.read()\n\tf.close()\n\n\treturn data\n\n\n'>>> content = get_bin_file.get_bin_file("./str_split.py.png")>>> print(content)b'\x89PNG\r\n\x1a\n\x00\x00\x00\rIHDR\x00\x00\x01\t\x00×××××××××××××××××××××××××××××××××××××\xb5\xeb\x93\xbc2\x00\x00\x00\x00IEND\xaeB`\x82'>>>
0 0
- Python小程序:获取二进制文件的所有内容
- Python小程序:获取文本文件的所有内容
- Python小程序:获取文本文件的所有内容
- python | 读入二进制文件生成曲线图的小程序
- Python显示二进制文件内容
- Python获取Redis所有Key以及内容
- 获取元素的所有内容
- C++程序显示二进制文件“内容”
- 小程序:获取动态库/程序依赖的所有动态库
- 获取WPF所有控件的模板内容
- 内容提供者获取手机所有的短信
- python的二进制文件读写
- 获取所有的th标签的内容,遍历内容
- 获取Bootstrap-Table的所有内容,修改行内容
- jquery获取table当前行的内容,table所有内容
- 获取Bootstrap-Table的所有内容,修改行内容
- Python爬虫小实践:获取某个网站所有的外部链接以及内部链接
- 根据取到文件的内容 生成二进制文件 并通过servlet获取
- bower install js使用bower管理js
- 欧拉计划 Java实现
- gcc创建,使用动态库
- 数字螺旋输出,O(1)空间复杂度解法
- adb的使用和一些常会用到的DOS/Linux命令
- Python小程序:获取二进制文件的所有内容
- JSP 9 大内置对象详解
- 计算机科学-第1周 三道C语言编程题
- Linux 进程通信
- c++中string类和vector类比较
- Unity3D适合初学者的书籍(下载链接)
- java对象之间的关系
- Android 常用 mimeType 表
- UBIFS分区制作 UBIFS烧写 启动 (转载)