I try to replace a few bytes of a binary file, but it does not do it correctly, in Python

0

Anyone with experience in file manipulation?

  • 1st Code block, create a text file.

  • 2nd Code block, replaces the first letters of the file leaving the rest intact. (ALL CORRECT)

    The input is only for pausing.

  • 3rd code block, try to "replace" the first three bytes of the file as well, but it does not do it well. I recorded those three bytes but erasing the rest of the file dwarfing those three bytes.

Is there a more correct way to replace binary bytes of a file leaving the rest intact?

#Escribir y crear si no existe con .write()
archivo_texto = open("archivo.txt", "w") # Nombre de archivo y modo de acceso

frase = "Estupendo día para estudiar Pyhon\n el miércoles"

archivo_texto.write(frase) # Lo graba

archivo_texto.close()

#Con esto escribimos reemplazando el texto en la posición determinada por ".seek()"
archivo_texto=open("archivo.txt", "r+")

archivo_texto.seek(0) # Esto lo que hace es posicionar el puntero donde queramos, al ser (0) nos imprime el texto desde el principio

frase="Reemplazamos"
archivo_texto.write(frase)
archivo_texto.close()


a = input("PULSA ENTER PARA CONTINUAR")


archivo_texto=open("archivo.txt", "wb") # En modo binario

archivo_texto.seek(0) # Esto lo que hace es posicionar el puntero donde queramos, al ser (0) nos imprime el texto desde el principio

frase=(b"\x01\x02\x03")

archivo_texto.write(frase)

archivo_texto.close()
    
asked by Nombre Apellido 11.11.2018 в 22:17
source

1 answer

0
archivo_texto=open("archivo.txt", "r+b") # En modo binario

I hope you serve bro

    
answered by 13.11.2018 в 17:16