从二进制文件中剥离第一个字节 [英] stripping the first byte from a binary file
问题描述
有人会介意我如何从
二进制文件中删除第一个字节吗?出于某种原因,我无法从我读过的二进制
文件编辑示例中看出来。谢谢。
~rv
Would someone mind showing me how to strip the first byte from a
binary file? For some reason I can''t figure this out from the binary
file editing examples I''ve read. Thanks.
~rvr
推荐答案
rvr写道:
rvr wrote:
有人会介意我如何从
二进制文件中删除第一个字节吗?出于某种原因,我无法从我读过的二进制
文件编辑示例中看出来。谢谢。
Would someone mind showing me how to strip the first byte from a
binary file? For some reason I can''t figure this out from the binary
file editing examples I''ve read. Thanks.
你的意思是这样的吗?
f = open(''test.dat'',''rb' ')
f.read(1)#读取第1个字节并忽略它
rest = f.read()#read rest
或
data = f.read()
data = data [1:] #skip 1st byte
?
-
Jeremy Sanders
http://www.jeremysanders.net/
7月10日下午6:37,Jeremy Sanders < jeremy
+抱怨... @ jeremysanders.netwrote:
On Jul 10, 6:37 pm, Jeremy Sanders <jeremy
+complangpyt...@jeremysanders.netwrote:
rvr写道:
rvr wrote:
有人会介意如何从
二进制文件中删除第一个字节吗?出于某种原因,我无法从我读过的二进制
文件编辑示例中看出来。谢谢。
Would someone mind showing me how to strip the first byte from a
binary file? For some reason I can''t figure this out from the binary
file editing examples I''ve read. Thanks.
你的意思是这样的吗?
f = open(''test.dat'',''rb' ')
f.read(1)#读取第1个字节并忽略它
rest = f.read()#read rest
或
data = f.read()
data = data [1:] #skip 1st byte
Do you mean something like this?
f = open(''test.dat'', ''rb'')
f.read(1) # read 1st byte and ignore it
rest = f.read() # read rest
or
data = f.read()
data = data[1:] # skip 1st byte
有没有办法编辑文件?我似乎能够做到的最好的事情就是使用你的第二个解决方案将文件读入字符串,然后重新打开文件写入并放入整个事情回来了(减去
第一个字节)。谢谢。
~rv
Is there a way to edit the file in place? The best I seem to be able
to do is to use your second solution to read the file into the string,
then re-open the file for writing and put the whole thing back (minus
the first byte). Thanks.
~rvr
2007年7月11日星期三01:06:04 +0000,rvr写道:
On Wed, 11 Jul 2007 01:06:04 +0000, rvr wrote:
有没有办法编辑文件到位?我似乎能够做的最好的事情就是用你的第二个解决方案将文件读入字符串,然后
重新打开文件写入并放入整回来(减去
第一个字节)。谢谢。
Is there a way to edit the file in place? The best I seem to be able to
do is to use your second solution to read the file into the string, then
re-open the file for writing and put the whole thing back (minus the
first byte). Thanks.
我不相信任何常用的流行操作系统
(Windows,Linux,Mac,* BSD)具有任何此类功能。
为了安全起见,最好将文件(减去第一个字节)复制到
临时文件,然后重命名副本超过原来的。这样,如果
你的进程在复制文件的中途死亡,你就不会丢失数据。
在Linux下重命名文件是原子的(可能是)Mac,所以它尽可能安全。即使在不具有原子性的Windows下,它也比使用文件的覆盖范围要小一些b $ b b / b。
-
史蒂文。
I don''t believe that any of the popular operating systems in common use
(Windows, Linux, Mac, *BSD) have any such functionality.
For safety, you are best off copying the file (minus the first byte) to a
temporary file, then renaming the copy over the original. That way if
your process dies midway through copying the file, you don''t lose data.
Renaming the file is atomic under Linux and (probably) Mac, so it is as
safe as possible. Even under Windows, which isn''t atomic, it has a
smaller margin for disaster than over-writing the file in place.
--
Steven.
这篇关于从二进制文件中剥离第一个字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!