使用Python分割和写入二进制文件

使用Python分割和写入二进制文件,python,binaryfiles,Python,Binaryfiles,我有两个二进制输入文件,firstfile和secondfile第二个文件是第一个文件+附加材料。我想在一个单独的文件中隔离此附加材料,newfile。这就是我到目前为止所做的: import os import struct origbytes = os.path.getsize(firstfile) fullbytes = os.path.getsize(secondfile) numbytes = fullbytes-origbytes with open(secondfile,'rb

我有两个二进制输入文件,
firstfile
secondfile
<代码>第二个文件是
第一个文件
+附加材料。我想在一个单独的文件中隔离此附加材料,
newfile
。这就是我到目前为止所做的:

import os
import struct

origbytes = os.path.getsize(firstfile)
fullbytes = os.path.getsize(secondfile)
numbytes = fullbytes-origbytes

with open(secondfile,'rb') as f:
    first = f.read(origbytes)
    rest = f.read()
当然,我倾向于做(这似乎是可行的):

我找不到它,但我想我读了下去,所以我应该在写入文件之前先使用
struct.pack
打包它。下面是一个错误:

with open(newfile,'wb') as f:
    f.write(struct.pack('%%%ds' % numbytes,rest))

-----> error: bad char in struct format
然而,这是可行的:

with open(newfile,'wb') as f:
    f.write(struct.pack('c'*numbytes,*rest))
对于那些有效的,这给了我正确的答案

with open(newfile,'rb') as f:
    test = f.read()

len(test)==numbytes

-----> True

这是写二进制文件的正确方法吗?我只想确保我正确地执行了这一部分,以诊断文件的第二部分是否已损坏,因为我输入的另一个读取器程序
newfile
告诉我,或者我做得不对。谢谢。

这不是c,格式字符串中没有%。你想要的是:

f.write(struct.pack('%ds' % numbytes,rest))
这对我很有用:

>>> struct.pack('%ds' % 5,'abcde')
'abcde'

说明:
'%%%ds'%15
'%15s'
,而您需要的是
'%15
'15s'

这不是c,格式字符串中没有%。你想要的是:

f.write(struct.pack('%ds' % numbytes,rest))
这对我很有用:

>>> struct.pack('%ds' % 5,'abcde')
'abcde'

说明:
'%%%ds'%15
'%15s'
,而您想要的是
'%15
,它是
'15s'
如果您知道第二个文件与第一个文件+附加数据相同,为什么还要读取第二个文件的第一部分

with open(secondfile,'rb') as f:
    f.seek(origbytes)
    rest = f.read()
至于写东西,

with open(newfile,'wb') as f:
    f.write(rest)

很好。带有
struct
的东西无论如何都是不可操作的。你唯一可以考虑的是<代码> REST < /代码>的大小。如果它可能很大,您可能希望以块的形式读取和写入数据

如果您知道secondfile与firstfile+附加数据相同,为什么还要读取secondfile的第一部分

with open(secondfile,'rb') as f:
    f.seek(origbytes)
    rest = f.read()
至于写东西,

with open(newfile,'wb') as f:
    f.write(rest)
很好。带有
struct
的东西无论如何都是不可操作的。你唯一可以考虑的是<代码> REST < /代码>的大小。如果它可能很大,您可能希望以块的形式读取和写入数据

  • 您不需要读取原始字节,只需将文件指针移到正确的位置:
    f.seek(numbytes)
  • 您不需要
    struct
    打包,而是将
    rest
    写入
    newfile
  • 您不需要读取原始字节,只需将文件指针移到正确的位置:
    f.seek(numbytes)
  • 您不需要
    struct
    打包,而是将
    rest
    写入
    newfile

  • 没有理由使用
    struct
    模块,该模块用于在二进制格式和Python对象之间进行转换。这里不需要转换

    Python2.x中的字符串只是一个字节数组,可以在文件中读写。(在Python3.x中,如果使用
    open(filename,'rb')
    打开文件,read函数将返回一个
    bytes
    对象,这与此相同)

    因此,您可以将文件读入字符串,然后再次写入:

    import os
    
    origbytes = os.path.getsize(firstfile)
    fullbytes = os.path.getsize(secondfile)
    numbytes = fullbytes-origbytes
    
    with open(secondfile,'rb') as f:
        first = f.seek(origbytes)
        rest = f.read()
    
    with open(newfile,'wb') as f:
        f.write(rest)
    

    没有理由使用
    struct
    模块,该模块用于在二进制格式和Python对象之间进行转换。这里不需要转换

    Python2.x中的字符串只是一个字节数组,可以在文件中读写。(在Python3.x中,如果使用
    open(filename,'rb')
    打开文件,read函数将返回一个
    bytes
    对象,这与此相同)

    因此,您可以将文件读入字符串,然后再次写入:

    import os
    
    origbytes = os.path.getsize(firstfile)
    fullbytes = os.path.getsize(secondfile)
    numbytes = fullbytes-origbytes
    
    with open(secondfile,'rb') as f:
        first = f.seek(origbytes)
        rest = f.read()
    
    with open(newfile,'wb') as f:
        f.write(rest)
    

    啊,好的,谢谢你--但是你知道这是不是分割二进制文件的正确方法吗?@cripledLambda不知道,但听起来它会工作,只要第二个文件只是第一个文件,末尾有更多数据。啊,对,谢谢--但是你知道这是否是分割二进制文件的正确方法吗?@SlippedLambda不知道,但听起来似乎可以,只要第二个文件只是第一个文件,末尾有更多数据。谢谢你对
    struct
    使用的说明。谢谢你对
    struct
    使用的说明。