0

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```
Community
  • 1
  • 1
v.tralala
  • 1,444
  • 3
  • 18
  • 39
  • Did you try to elevate the permissions on the file and file directory itself to 700 or higher? – dmitryro Jun 26 '16 at 18:17
  • no I haven't tried that. I assume it would work too. I already found a fix by calling f.truncate() before calling DOMTree.writexml(f). My problem here is just to understand why there is an xml parse error when writing without truncating and why this truncating has no visible difference. – v.tralala Jun 26 '16 at 20:52
  • This might be helpful http://stackoverflow.com/questions/22142243/open-file-for-read-write-create-if-needed – dmitryro Jun 26 '16 at 21:10
  • Well I recognized that I've overseen, that without truncation there was some additional characters at the end which would not match the xml-syntax. At the end something like this would appear, which I don't understand: '... ion>' – v.tralala Jul 10 '16 at 19:23

0 Answers0