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:
.... bdata = bytes(tmpdata)
and then write bdata to a new file?
Or is my logic wrong or using unnecessary steps?
(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 assignmentI 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 errorsSo 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?