python 5-2-2 如何使用array.array(‘h’,(0 for _ in xrange(n)))/buf.tofile(f2)
import array
f = open(“d:\demo.wav”,”rb”) ==>通过二进制打开文件
info = f.read(44) ==>读取文件前44个字节
f.seek(0,2) ==>将文件指针直到文件末尾
n = (f.tell() - 44)/2 ==>f.tell()获得文件长度
buf = array.array(‘h’,(0 for _ in xrange(n))) ==>初始化数组
f.seek(44) ==>文件指针指到第44个字节
f.readinto(buf) ==>将文件44个字节以后的内容写入到buf中
f.close() ==>
for i in xrange(n):buf[i] /= 8 ==》将每个数字除以8
f2 = open(“d:\test2.wav”,”wb”)
f2.write(info) ==>先将文件的前44个字节写入到文件,
buf.tofile(f2) ==》将修改过的文件写入到新文件
f2.close()
help(buf.tofile)
help(buf.tofile)
Help on built-in function tofile:
tofile(…)
tofile(f)
Write all items (as machine values) to the file object f. Also called as
write.
>
>f = open(“d:\demo.wav”,”rb”)
help(f.readinto)
Help on built-in function readinto:
readinto(…)
readinto() -> Undocumented. Don’t use this; it may go away.
>