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 11 10622
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.
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
?
--
Jeremy Sanders http://www.jeremysanders.net/
On Jul 10, 6:37 pm, Jeremy Sanders <jeremy
+complangpyt... @jeremysanders. netwrote:
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.
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
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
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.
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.
On Jul 11, 1:28 pm, Steven D'Aprano
<ste...@REMOVE. THIS.cybersourc e.com.auwrote:
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.
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.
Thanks for your response. While searching for solution, I found this: http://mail.python.org/pipermail/pyt...er/116519.html
Quoting from it:
"""
Replace 2 bytes in place beginning at offset 100 (101st byte):
f = open('text_inpu t', 'r+b')
f.seek(100)
f.write(chr(123 ) + chr(0x80))
f.seek(0,2)
f.close()
"""
Can I use the seek() and write() methods in a similar way to remove
the first byte? For whatever reason I can't seem to make it work
myself. Thanks again.
~rvr
rvr wrote:
On Jul 11, 1:28 pm, Steven D'Aprano
<ste...@REMOVE. THIS.cybersourc e.com.auwrote:
>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.
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.
Thanks for your response. While searching for solution, I found this:
http://mail.python.org/pipermail/pyt...er/116519.html
Quoting from it:
"""
Replace 2 bytes in place beginning at offset 100 (101st byte):
f = open('text_inpu t', 'r+b')
f.seek(100)
f.write(chr(123 ) + chr(0x80))
f.seek(0,2)
f.close()
"""
Can I use the seek() and write() methods in a similar way to remove
the first byte? For whatever reason I can't seem to make it work
myself. Thanks again.
Funny. I just happened to read ESR's "how to ask questions the smart way" and
your posts match quite a few of the examples. :)
No, you can't. Steven's solution is what I'd go for.
Stefan
On Jul 11, 1:25 pm, Stefan Behnel <stefan.behne l-n05...@web.dewr ote:
rvr wrote:
On Jul 11, 1:28 pm, Steven D'Aprano
<ste...@REMOVE. THIS.cybersourc e.com.auwrote:
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.
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.
Thanks for your response. While searching for solution, I found this:
http://mail.python.org/pipermail/pyt...er/116519.html
Quoting from it:
"""
Replace 2 bytes in place beginning at offset 100 (101st byte):
f = open('text_inpu t', 'r+b')
f.seek(100)
f.write(chr(123 ) + chr(0x80))
f.seek(0,2)
f.close()
"""
Can I use the seek() and write() methods in a similar way to remove
the first byte? For whatever reason I can't seem to make it work
myself. Thanks again.
Funny. I just happened to read ESR's "how to ask questions the smart way" and
your posts match quite a few of the examples. :)
No, you can't. Steven's solution is what I'd go for.
Stefan
Forgive my newbie ignorance, but I am wondering why the other method
would not work? I mean it may not be very safe,
but I guess it may perform a lot better, than having to read the whole
file just to cut out the first byte.
TIA,
../alex
--
..w( the_mindstorm )p.
>
Forgive my newbie ignorance, but I am wondering why the other method
would not work? I mean it may not be very safe,
but I guess it may perform a lot better, than having to read the whole
file just to cut out the first byte.
Because seeking is not moving? Shifting data bytewise isn't something that
is supported by the underlying OS filesystems, and thus not supported. But
replacing bytes with others is. Which seek is for.
Diez
Alex Popescu wrote:
Forgive my newbie ignorance, but I am wondering why the other method
would not work? I mean it may not be very safe,
but I guess it may perform a lot better, than having to read the whole
file just to cut out the first byte.
Why would you expect that? It *might* perform better if there was a system
call for removing bytes from inside a file, as that could reduce the
intermediate space requirements to the size of a hard disk sector rather than
the remaining size of the file (note that the time consumption would not be
reduced significantly, if the remaining file has to be copied around to fill
the sectors). But since that is a rather rare use case that most people would
prefer being handled in a safe rather than space-optimal way, I don't see the
need for such a function.
Stefan
On Jul 11, 4:15 pm, "Diez B. Roggisch" <d...@nospam.we b.dewrote:
Forgive my newbie ignorance, but I am wondering why the other method
would not work? I mean it may not be very safe,
but I guess it may perform a lot better, than having to read the whole
file just to cut out the first byte.
Because seeking is not moving? Shifting data bytewise isn't something that
is supported by the underlying OS filesystems, and thus not supported. But
replacing bytes with others is. Which seek is for.
Diez
As far as I know seek is just about positioning and nothing else.
So, in fact the problem boils down to os support for deleting a bytes.
bests,
../alex
--
..w( the_mindstorm )p. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics |
by: Albert Tu |
last post by:
Hi,
I am learning and pretty new to Python and I hope your guys can give me
a quick start.
I have an about 1G-byte binary file from a flat panel x-ray detector; I
know at the beggining there is a 128-byte header and the rest of the
file is integers in 2-byte format.
What I want to do is to save the binary data into several smaller files
|
by: J. Campbell |
last post by:
OK...I'm in the process of learning C++. In my old (non-portable)
programming days, I made use of binary files a lot...not worrying
about endian issues. I'm starting to understand why C++ makes it
difficult to read/write an integer directly as a bit-stream to a file.
However, I'm at a bit of a loss for how to do the following. So as
not to obfuscate the issue, I won't show what I've been attempting ;-)
What I want to do is the...
|
by: Arnold |
last post by:
I need to read a binary file and store it into a buffer in memory (system
has large amount of RAM, 2GB+) then pass it to a function. The function
accepts input as 32 bit unsigned longs (DWORD). I can pass a max of 512
words to it at a time. So I would pass them in chunks of 512 words until the
whole file has been processed. I haven't worked with binary files before so
I'm confused with how to store the binary file into memory. What sort of...
|
by: Arnold |
last post by:
Is using fseek and ftell a reliable method of getting the file size on a
binary file? I thought I remember reading somewhere it wasn't... If not what
would be the "right" and portable method to obtain it? Thanks.
|
by: DBC User |
last post by:
Hi Sharpies,
I have a C program I am converting it into C#. Everything is fine
except this process creates a 6K byte binary file. This file initially
filled with 6K null and then start populating only the fields with
value in specified locations(3 seperate structures). The way the C does
is by creating a structures, which will be filled with null initially.
Then selectivly populating only the fields with values and only up to
the length...
| |
by: Adam J. Schaff |
last post by:
I am writing a quick program to edit a binary file that contains file paths
(amongst other things). If I look at the files in notepad, they look like:
<gibberish>file//g:\pathtofile1<gibberish>file//g:\pathtofile2<gibberish>
etc.
I want to remove the "g:\" from the file paths. I wrote a console app that
successfully reads the file and writes a duplicate of it, but fails for some
reason to do the "replacing" of the "g:\". The code...
|
by: John Dann |
last post by:
I'm trying to read some binary data from a file created by another
program. I know the binary file format but can't change or control the
format. The binary data is organised such that it should populate a
series of structures of specified variable composition.
I have the structures created OK, but actually reading the files is
giving me an error. Can I ask a simple question to start with:
I'm trying to read the file using the...
|
by: Mad Scientist Jr |
last post by:
i'm trying to read a file byte by byte (and later alter the data and
write it to a 2nd file byte by byte) and running into a problem where
it seems to keep reading the same byte over and over again (an endless
loop). i thought that BinaryReader.ReadByte advanced to the next byte?
i had it time out after 1000 iterations, and keeps outputting the same
byte. any help appreciated, my code is below:
Imports System.io
|
by: Jack |
last post by:
Hi
I am having a little trouble trying to read a binary file, I would like
to write an ascii to Metastock converter in python but am not having a
lot of success.
The file formats are
http://sf.gds.tuwien.ac.at/00-pdf/m/mstockfl/MetaStock.pdf
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look !
Part I. Meaning of...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed.
This is as boiled down as I can make it.
Here is my compilation command:
g++-12 -std=c++20 -Wnarrowing bit_field.cpp
Here is the code in...
| |
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth.
The Art of Business Website Design
Your website is...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
|
by: conductexam |
last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one.
At the time of converting from word file to html my equations which are in the word document file was convert into image.
Globals.ThisAddIn.Application.ActiveDocument.Select();...
|
by: 6302768590 |
last post by:
Hai team
i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
|
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |
by: bsmnconsultancy |
last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...
| |