heycooldude
heycooldude

Reputation: 321

Python:How to remove file extension from tag name in xml file

This is my code

from xml.dom import minidom
import os, shutil
os.chdir("E:\\")
xmldoc = minidom.parse('123.xml')
itemlist = xmldoc.getElementsByTagName('File') #TagName
print len(itemlist)
print itemlist[0].attributes['name'].value
for s in itemlist :
    shutil.move(s.attributes['name'].value, "E:\\Junk\\") #Moving to another location

The above code finds the tag and gets it value.

My xml file is something like this

<File>
   <File name="E:\qwer\abc.exe"/>
   <File name="E:\qwer\xyz.dll"/>
   <File name="E:\pqr\bc.exe"/>
</File>

How do I remove the "abc.exe"? Is there any way to search for the extension in the xml and then remove it? Final xml should be like this

<File>
   <File name="E:\qwer\"/>
   <File name="E:\qwer\"/>
   <File name="E:\pqr\"/>
</File>

Sorry if my question sound ambiguous

Upvotes: 0

Views: 405

Answers (1)

Noufal Ibrahim
Noufal Ibrahim

Reputation: 72855

Use os.path.dirname. It will give you just the directory. The other functions in os.path give you other pieces of the absolute path.

Upvotes: 3

Related Questions