Python Forum
Correct way to change bytes in a file?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Correct way to change bytes in a file?
#11
I've merged the threads, since they're still the same topic / subject.
Reply
#12
You can use bytearray instead of list (bytearray is mutable version of bytes and has some useful functions).

with open('data.data', 'rb') as f:
    data = bytearray(f.read())

data[:2] = 0, 0

with open('data.data', 'wb') as f:
    f.write(data)
Reply
#13
(Feb-22-2017, 11:04 PM)zivoni Wrote: You can use bytearray instead of list (bytearray is mutable version of bytes and has some useful functions).

with open('data.data', 'rb') as f:
    data = bytearray(f.read())

data[:2] = 0, 0

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

zivoni,

I had seen bytearray during my research but the "array" in the name threw me off since I was thinking of multi-dimensional arrays. (Which is not what I wanted.)  I didn't think of a "one-dimensional" array.  After your post, Googling "Python bytearray", I understand how it works now and that it can work like "bytes()" except it is mutable and also has more capability.

I put it in my code and it does just what I need.  I tried to find a way to mark your post as the solution but could not find a way to do that.  I also couldn't find how to "like" your post, but I know it's possible since others have liked it.  Anyway, Thanks!

BTW, for lurkers just learning Python:
I had borrowed 7 books about learning Python from our state library and none cover bytearray().  I only found it online by Googling.
Reply
#14
Quote:I had seen bytearray during my research but the "array" in the name threw me off since I was thinking of multi-dimensional arrays.
In Python and array(like named Java/C++/C) is called a list.
bytearray() is used in more specialized cases,so books can lack info about it.
There are very good books that has info/usage about it.
Like Fluent Python and Python Cookbook, 3rd Edition and general info in doc.
Reply
#15
Python is considered to be "high level language with batteries included" and direct manipulations with binary data are often left to specialized libraries, thats perhaps reason why some introductory texts just mention bytes and decode/encode and go away.

In that sense bitwise operations are similar, they are usually mentioned, but rarely used.
Reply
#16
(Feb-23-2017, 10:52 AM)snippsat Wrote: In Python and array(like named Java/C++/C) is called a list.
Python's list is closer to a C++ vector or Java ArrayList, C/C++/Java arrays are lower-level than Python if only because they do not automatically resize themselves.
Reply
#17
(Feb-23-2017, 10:52 AM)snippsat Wrote: In Python and array(like named Java/C++/C) is called a list.
bytearray() is used in more specialized cases,so books can lack info about it.
There are very good books that has info/usage about it.
Like Fluent Python and Python Cookbook, 3rd Edition and general info in doc.

Thank you for listing the two books with info on bytearray().  I checked our state library's online catalog and it has Fluent Python so I'll request it.

All the best,
Raptor88

(Feb-23-2017, 11:29 AM)zivoni Wrote: Python is considered to be "high level language with batteries included" and direct manipulations with binary data are often left to specialized libraries, thats perhaps reason why some introductory texts just mention bytes and decode/encode and go away.

In that sense bitwise operations are similar, they are usually mentioned, but rarely used.

Hi zivoni,

That is my disappointment with my initial online research when looking for the easiest programming language to learn that covers byte manipulation to graphic user interfaces.  It took a LOT of digging before I learned that Python could do both easily.  (And I didn't even know of Python's existence when I started my research, ha, ha.) For instance, I wanted to know how easy it could be to do a folder/file search but I had to learn of tkinter's existence and then when googling that keyword, I discovered how powerful tkinter is for graphic user interface that can do that. Just a short paragraph of what Python can cover would have been extremely helpful for me to decide to learn Python.

After deciding on Python and borrowing 7 books from the library, NONE let the user know the extremes that Python can cover.  bytes() or bytearray() are not even listed in the indexes.  Not asking for any detail on those methods but at least let the reader know of their existence and a sentence or two of their capability.  I hope future writers of Python introductory books will let the reader know of Python's bit manipulation to GUI libraries capability in their introductory paragraphs.  Also list the built-in types and methods available at the end of the book even if the book does not cover them.  Give the newbie the keywords to Google online since they have no clue which keywords to search for.

Thanks for your input,
Raptor88 (end of rant  Wink )
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Question [SOLVED] Correct way to convert file from cp-1252 to utf-8? Winfried 8 545 Feb-29-2024, 12:30 AM
Last Post: Winfried
  logging: change log file permission with RotatingFileHandler erg 0 959 Aug-09-2023, 01:24 PM
Last Post: erg
  How can I change the uuid name of a file to his original file? MaddoxMB 2 874 Jul-17-2023, 10:15 PM
Last Post: Pedroski55
  Change HID bytes using pywinusb.hid Stealthrt 0 589 Jul-06-2023, 03:36 PM
Last Post: Stealthrt
  unittest generates multiple files for each of my test case, how do I change to 1 file zsousa 0 918 Feb-15-2023, 05:34 PM
Last Post: zsousa
  find some word in text list file and a bit change to them RolanRoll 3 1,482 Jun-27-2022, 01:36 AM
Last Post: RolanRoll
Photo (Beginners problem) Does file change during this code? fiqu 3 1,849 Nov-03-2021, 10:23 PM
Last Post: bowlofred
  change csv file into adjency list ainisyarifaah 0 1,480 Sep-21-2021, 02:49 AM
Last Post: ainisyarifaah
  Use Python to change a PHP file on my little webpage Pedroski55 0 1,481 Aug-28-2021, 12:42 AM
Last Post: Pedroski55
  Get amount of bytes in a file chesschaser 1 1,544 Aug-23-2021, 03:24 PM
Last Post: deanhystad

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020