0

I want to replace text of Houdini file(.hip) which contains mixture of Binary code and Text. I have python code which replace text file. When i try to replace Text in Houdini file the file gets Corrupt after replacement.

Can anyone tell me How to replace text in Houdini file without corrupting it?

code:

import fileinput,sys
for line in fileinput.input("file_name",inplace=True):
    line = line.replace("from","to")
    sys.stdout.write(line)

When i try to replace Houdini file(.hip) using this code then file gets corrupt.

Do anyone know how to replace Houdini file without opening it ?

python_fan
  • 113
  • 2
  • 15

1 Answers1

0

I came across this problem recently and tried the suggestions mentioned in the comments.
Here is the final solution that worked for me:

import re
def UpdateFile(self, file, oldstrg, newStr):
    bytetofind = bytes(oldstrg, encoding='utf-8')
    bytetoreplace = bytes(newStr, encoding='utf-8')
    f = open(file, 'rb+')
    text = f.read()
    text = re.sub(bytetofind , bytetoreplace, text, count=1)
    f.seek(0)
    f.write(text)
    f.close()

The count=1 is there because I wanted to replace only a single instance of that string.
You can change/remove as per your requirements.

סטנלי גרונן
  • 2,917
  • 23
  • 46
  • 68
hussAtt
  • 1
  • 2