I am trying to modify some eclipse ".project" files to be able to rename those projects. However several strange Erros occured to me. I recevied a project file, which was hidden, so that I would get the Errno 13 for the open mode 'w' (see IOError: [Errno 13] Permission denied when trying to open hidden file in “w” mode). So I changed the mode to "r+", where the difference is just the missing truncation in the beginning and an additional read-possiblity which I don't use. But then the call DOMTree.writexml(f) only works for the first iteration. If I try to modify the files again, I get an error: "xml.parsers.expat.ExpatError: junk after document element: line 16, column 21". I checked the files and line 16 is just the end of the file and I can't see any difference between the two modes "r+" and "w". So why is there an xml parser error when using the mode "r+" and how is truncating before writing important, if I can't see any difference (even in a HEX-Editor)?
The algorithm looks as follows, where PFile is some path to the project file, ProjDir is the path to some directory which contains the project file.
def modify(PFile, ProjDir, path):
DOMTree = DOM.parse(PFile)
ProjDescription= DOMTree.documentElement
name = ProjDescription.getElementsByTagName('name')[0]
# rename Project
temp1 = ProjDir.split("--")
if len(temp1) >1:
temp2 ="--".join(temp1[-2:])
temp2 = temp1[1][:-10]+"--"+temp2
name.childNodes[0].data = temp2
print("Project name: %s"% name.childNodes[0].data)
else:
print(ProjDir+" not modified")
return 0
f = open(PFile,'r+') #errno13 in mode w if file is hidden, in mode r+ truncate is missing?!
#f.truncate();
DOMTree.writexml(f)
f.close()
return 0```
Aucun commentaire:
Enregistrer un commentaire