#!/usr/bin/env python
infile = file("in.mp3","rb")
outfile = file("out.txt","wb")
def main():
while 1:
c = infile.read(1)
if not c:
break
outfile.write(hex(ord(c)))
outfile.close()
infile.close()
if __name__ == '__main__':
main()

下面是我自己改过的

#coding:utf-8
# 程序目标,读取1.bmp,然后以16进制方式写到txt文件 def main():
f = open("1.bmp","rb")
outfile = open("out.txt","wb")
i = 0
while 1:
c = f.read(1)
i = i + 1
if not c:
break
if i%32 == 0:
outfile.write("\n")
else:
if ord(c) <= 15:
outfile.write("0x0"+hex(ord(c))[2:]+" ")
else:
outfile.write(hex(ord(c))+" ")
outfile.close()
f.close() if __name__=="__main__":
main()

效果如下:

当然,我需要的是真正的十六进制值。然后代码变成了这样

#coding:utf-8
# 程序目标,读取1.bmp,然后以16进制方式写到txt文件 def main():
f = open("1.bmp","rb")
outfile = open("out.txt","wb")
i = 0
while 1:
c = f.read(1)
i = i + 1
if not c:
break
if i%32 == 0:
outfile.write("\n")
else:
if ord(c) <= 15:
outfile.write(("0x0"+hex(ord(c))[2:])[2:]+" ")
else:
outfile.write((hex(ord(c)))[2:]+" ")
outfile.close()
f.close() if __name__=="__main__":
main()

文件的数据变成了

python 读取文件、并以十六进制的方式写入到新文件的更多相关文章

  1. 文件的三种打开方式及with管理文件上下文

    文件的三种打开方式及with管理文件上下文 一.文件的三种打开方式 1.1 只读 f = open(r'D:\pycharm\yjy\上海python学习\456.txt','r',encoding= ...

  2. 监控文件内容变化,即时写入到新文件(tail)

    监控文件a,如有新内容写入,即时将新内容写入到新文件aa中: fw=open('e:\\aa.txt','ab') with open('e:\\a.txt','rb') as fo: while T ...

  3. 按行读取.txt文件,并按行写入到新文件中

    package com.test.io; import java.io.BufferedReader; import java.io.BufferedWriter; import java.io.Fi ...

  4. python从命令窗口启动脚本 创建并写入内容到文件示例

    写入到文件示例: #!/usr/bin/env python3 from math import exp, log, sqrt import re from datetime import date, ...

  5. [C/C++][文件操作] 对比目录并列出同名较新文件、较旧文件 0.1

    主要是模仿robocopy的部分功能 (robocopy /L 参数可以列出本地目录和备份目录中的异同之处,主要是标记出:较新的.较旧的.多出的文件 ) 现在还不会写GUI,打算后面自己做目录树dif ...

  6. SparkStreaming python 读取kafka数据将结果输出到单个指定本地文件

    # -*- coding: UTF-8 -*- #!/bin/env python3 # filename readFromKafkaStreamingGetLocation.py import IP ...

  7. python读取绝对路径的三种方式

    import pandas as pd dood_inf0=pd.read_csv("C:\\Users\\Administrator\\Desktop\\food_info.csv&quo ...

  8. Node.js读取某个目录下的所有文件夹名字并将其写入到json文件

    针对解决的问题是,有些时候我们需要读取某个文件并将其写入到对应的json文件(xml文件也行,不过目前用json很多,json是主流). 源码如下:index.js var fs = require( ...

  9. CMD批处理把txt文本中的每行写入一个新文件,第一列作文件名

    需求 现在有一个文件格式如图 ID 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17001 89.84 8.87 1.29 -0.0 0.0 68.99 0.0 0. ...

随机推荐

  1. RESTFul中的那些事(2)----怎样支持RESTFul的HTTP Patch方法?

    我们在调用RESTFul服务的时候,有的时候.第三方的服务会提供支持PATCH 操作的方法,在这样的情况下.我们假设我们以下的这样的方式, 去调用PATCH操作.肯定会返回40X的错误. PATCH ...

  2. logging日志管理--将日志打印在屏幕上

    # -*- coding: cp936 -*- # test.py #http://blog.chinaunix.net/uid-27571599-id-3492860.html #logging日志 ...

  3. 类的专有方法(__len__)

    # -*- coding: utf-8 -*- #python 27 #xiaodeng #http://www.imooc.com/code/6252 #类的专有方法(__len__) #如果一个类 ...

  4. echo “新密码”|passwd --stdin 用户名

    --stdin This option is used to indicate that passwd should read the new password from standard input ...

  5. WinForm如何调用Web Service

    参考地址 今天看了李天平关于WinForm调用Web Service的代码,我自己模仿做一个代码基本都是复制粘贴的,结果不好使.郁闷的是,又碰到那个该死的GET调用Web Service,我想肯定又是 ...

  6. Matlab调用返回游标的存储过程的分析和处理

    2.Matlab调用Oracl带游标参数输出的存储过程 笔者也是将工作之中遇到的问题进行了搜集与整理,才完成该文的编写,希望能帮助到有需要的朋友. 2.1.PLSQL中的存储过程 PROCEDURE ...

  7. Foundations of Machine Learning: Rademacher complexity and VC-Dimension(2)

    Foundations of Machine Learning: Rademacher complexity and VC-Dimension(2) (一) 增长函数(Growth function) ...

  8. Nginx配置https和wss

    微信小程序不仅要求必须是HTTPS和WSS,还要求URL里不能有端口号. 一.使用Nginx足够了 常见的服务器有三种: Nginx IIS Apache 这三种服务器都可以配置https,但是没必要 ...

  9. VMware workstation 与 VMware GSX Server 的区别

    http://xsagaplus.iteye.com/blog/358917VMware是提供一套虚拟机解决方案的软件,主要产品分为如下三个. VMware-ESX-Server  这个版本并不需要操 ...

  10. 【js】indexOf()

    /** **位置方法indexOf()和lastIndexOf() **这两个方法都接收两个参数:要查找的项和(可选的)表示查找起点位置的索引 **indexOf()方法从数组的开头(位置0)开始向后 ...