Python读写二进制文件(改变指定位置的值)

直接上代码了:

 

import sys
import os
#coding=utf-8

if __name__ == '__main__':
    #Create the main application
    try:

        outfile1 = open(r"./testfile1","wb")
        for i in range(0,64):
            outfile1.write(chr(0xee))
        print "write done!"
        outfile1.flush()
        outfile1.close()
        print "the file size is:", os.path.getsize(r'./testfile1')
        """0x20 write 0xff"""
        tp_file= open(r"./testfile1","rb+")
        tp_file.seek(0x20,0)
        tp_file.write(chr(0xff))
        tp_file.flush()
        tp_file.close()

        
    except:
        print("error.....")
        pass

 

 

查看结果:

posted @ 2015-03-30 20:04  JustRelax  阅读(3009)  评论(0)    收藏  举报