Ok so I got it working using this:
def replace_in_file(filename, idx, new_line):
f = open(filename, "r")
lines = f.readlines()
f.close()
lines[idx] = new_line + "\n"
f = open(filename, "w")
for line in lines:
write=line
f.write(write)
f.close()
files = os.listdir(root.dirname)
files = [s for s in files if ".xml" in s]
for x in range(len(files)):
#with open(os.path.join(root.dirname,files[x]),"r+") as f:
file = os.path.join(root.dirname,files[x])
print("Fixing "+file)
replace = '<?xml-stylesheet type="text/xsl" href="'+root.filename+'"?>'
replace_in_file(file, 0, replace)
with open(os.path.join(root.dirname,files[x]),"r+") as f:
old = f.read()
f.close
with open(os.path.join(root.dirname,files[x]),"w") as f:
f.write('<?xml version="1.0" encoding="UTF-8"?>\n'+old)
f.close