2013-12-12 128 views
0
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 

上面的代碼找到標記並獲取它的值。Python:如何從xml文件的標記名稱中刪除文件擴展名

我的XML文件是這樣

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

如何刪除 「abc.exe」?有沒有什麼方法可以在xml中搜索擴展名,然後將其刪除? 最終的XML應該是這樣的

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

很抱歉,如果我的問題聽起來含糊

回答

3

使用os.path.dirname。它會給你只是目錄。 os.path中的其他函數爲您提供絕對路徑的其他部分。

+0

這是OP更新他的問題後澄清的正確答案。 –

+0

非常感謝。它起作用 – heycooldude

+0

如果腳本在UNIX計算機上運行,​​但路徑名是Windows路徑,這可能會搞砸。 –