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?
#8
I started a different thread named "What is actually strored in memory?" but after 93 views, no answers so I'll present my actual task to hopefully get some answers.
(EDIT:  Opps, I see now that there are answers to that thread.  Thanks for those replies.  But please help me with this thread.  Thanks from a newbie.)

I want to read a file into memory, change some bytes in the memory data, then write the memory data to a new file.  I will not be "inserting" bytes but just changing byte values.  Here's some sample code:

with open("test 18.vf", "rb") as bfile:     # Just opens the file. Does not read the file.
    bdata = bfile.read()

bdata[2] = 10
TypeError: 'bytes' object does not support item assignment
I learned that "bdata" is a "bytes" type so is immutable.  If I convert "bdata" using the list() method, then I can change bytes without error.  Here's an example:

with open("test 18.vf", "rb") as bfile:     # Just opens the file. Does not read the file.
    bdata = bfile.read()

tmpdata = list(bdata)
tmpdata[2] = 10     

# This works with no errors
So after the bytes are changed in "tmpdata", is it correct that I need to convert it back to "bytes" data using this code:
.... bdata = bytes(tmpdata)
and then write bdata to a new file?

Or is my logic wrong or using unnecessary steps?
Reply


Messages In This Thread
What is actually stored in memory? - by Raptor88 - Feb-22-2017, 10:49 AM
RE: What is actually stored in memory? - by Mekire - Feb-22-2017, 10:59 AM
RE: What is actually stored in memory? - by Ofnuts - Feb-22-2017, 11:41 AM
RE: What is actually stored in memory? - by wavic - Feb-22-2017, 11:54 AM
RE: What is actually stored in memory? - by Ofnuts - Feb-22-2017, 04:50 PM
Correct way to change bytes in a file? - by Raptor88 - Feb-22-2017, 07:40 PM
RE: What is actually stored in memory? - by wavic - Feb-22-2017, 07:45 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  What does .flush do? How can I change this to write to the file? Pedroski55 3 293 Apr-22-2024, 01:15 PM
Last Post: snippsat
Question [SOLVED] Correct way to convert file from cp-1252 to utf-8? Winfried 8 1,110 Feb-29-2024, 12:30 AM
Last Post: Winfried
  logging: change log file permission with RotatingFileHandler erg 0 1,103 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 984 Jul-17-2023, 10:15 PM
Last Post: Pedroski55
  Change HID bytes using pywinusb.hid Stealthrt 0 675 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 989 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,582 Jun-27-2022, 01:36 AM
Last Post: RolanRoll
Photo (Beginners problem) Does file change during this code? fiqu 3 1,932 Nov-03-2021, 10:23 PM
Last Post: bowlofred
  change csv file into adjency list ainisyarifaah 0 1,528 Sep-21-2021, 02:49 AM
Last Post: ainisyarifaah
  Use Python to change a PHP file on my little webpage Pedroski55 0 1,533 Aug-28-2021, 12:42 AM
Last Post: Pedroski55

Forum Jump:

User Panel Messages

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