0

I guess I am doing something wrong.
I am not sure what it is though, but I keep getting TypeError: expected a character buffer object

I just want to open a file, seek to certain offsets and overwrite data from patch1 and patch2.

Here is the code I am using, please help me and show me what I am doing wrong:

patch1 = open("patch1", "r");
patch2 = open("patch2", "r");
main = open("patchthis.bin", "w");

main.seek(0xC0010);
main.write(patch1);
main.seek(0x7C0010);
main.write(patch1);
main.seek(0x40000);
main.write(patch2);
main.close();

I am noob when it comes to file handling with python, even though I have read up about it.
I really want to start learning more, but I need some good examples and any help sure would be appreciated :)

pradyunsg
  • 18,287
  • 11
  • 43
  • 96
james28909
  • 554
  • 2
  • 9
  • 20
  • this is PYTHON you don't need the semi-colons – pradyunsg Feb 01 '13 at 04:56
  • There's no need to add a semicolon at the end of each line. As long as you only have one statement per line (and you really shouldn't do otherwise), semicolons are unnecessary and redundant. – Nathan Feb 01 '13 at 05:11

1 Answers1

4

You are trying to write file object into file, not a string. try:

patch1_text = patch1.read()
main.write(patch1_text)

and so on.

Also use with statement when operating on files:

with open('patch1', 'r') as patch1:
    patch1_text = patch1.read()
    patch1.close()

And don't use semi-colons at the end of line !!!

Rafał Łużyński
  • 7,083
  • 5
  • 26
  • 38
  • i still am not getting the desired results :( i can write to a file but for some reason i cannot open a file set the cursor to 0xc0010 and write patch1. i know its prob easy but i just can wrap my head around writing to offsets :0 – james28909 Feb 01 '13 at 08:20
  • I can't help you if you wont give more detailed information. What error you get? – Rafał Łużyński Feb 02 '13 at 18:06
  • you were correct m8, i apologize for not giving you credit. i was doing what you said about trying to write a string when i really wanted to write a file. i have figured it out after all. thanks for your help m8 :) – james28909 Feb 03 '13 at 19:11