2012-01-23 26 views
2

讀取相同的文件名我有一個名爲junit.xml在同一目錄作爲我的腳本一個XML文件,我可以做解析它:從多個位置

xml_file = os.path.abspath(__file__) 
xml_file = os.path.dirname(xml_file) 
xml_file = os.path.join(xml_file, "junit.xml") 
root = ET.parse(xml_file).getroot(); # Where ET is the element tree 

,一切都OK。

但是,我有一個更復雜的例子,我需要解析一堆名爲「junit.xml」的文件,這些文件連續位於不同的目錄中。

的目錄如下:

\myhome\ireland\modules\builds\date1 
\myhome\ireland\modules\builds\date2 
\myhome\england\modules\builds\date1 
\myhome\england\modules\builds\date2 
\myhome\scotland\modules\builds\date1 
\myhome\scotland\modules\builds\date2 
\myhome\wales\modules\builds\date1 
\myhome\wales\modules\builds\date2 
\myhome\germany\modules\builds\date1 
\myhome\germany\modules\builds\date2 

現在,每個目錄有XML文件的集合。我只是想獲得的所有下junit.xml命名的文件:

\myhome\ireland\modules\builds\date2 
\myhome\england\modules\builds\date2 
\myhome\scotland\modules\builds\date2 

我怎樣才能做到這一點在Python的方式,其中,當我需要,我可以改變國家的名字和日期?

回答

4

使用字符串模板的路徑,例如:

path = r"\myhome\{}\modules\builds\date{}" 

以後可以使用通過str.format()功能(例如path.format("ireland", 1))來構造真實路徑。

然後,您可以遍歷國名和日期,併爲每一個解析XML文件:

for country in ["ireland", "england", "scotland"]: 
    for num in [1, 2]: 
     parse_xml(path.format(country, num)) 

哪裏parse_xml是得到一個路徑到XML文件並分析它的功能定義。

+0

我認爲目錄應該'目錄= 「\\ MYHOME \\ {0} \\模塊\\構建\\日期{1}」' – RanRag

+0

@RanRag,因爲Python 2.7(以及在Python 3 .x),位置參數可以隱式引用,即如果使用'{}',那麼將採用'str.format'的下一個參數。這在Python [格式字符串文檔](http://docs.python.org/library/string.html#formatstrings)中進行了解釋。 – spatz

+0

好吧,我正在使用Python 2.6,這就是爲什麼我必須使用我的版本的目錄。 – RanRag

2

首先,定義「模板」,你的文件會隨之而來,那麼國家名單和日期的列表:

dir_template = r'\myhome\%(country)s\modules\builds\%(date)s\junit.xml' 
countries = ['ireland', 'england', 'scotland', 'wales', 'germany'] 
dates = ['date1', 'date2'] 

for c in countries: 
    for d in dates: 
     xml_file = dir_template % {'country': c, 'date': d} 
     root = ET.parse(xml_file).getroot() 
     # ... 
0

不那麼有效,因爲包含候選目錄列表事前,但你可以還找junit.xml文件遞歸使用os.walk如下:

import os 

def get_junit_filenames(directory): 
    for dirpath, dirnames, filenames in os.walk(directory): 
     if 'junit.xml' in filenames: 
      yield os.path.join(dirpath, 'junit.xml') 

for filename in get_junit_filenames('/myhome'): 
    <process file> 

這樣你就不必擔心添加/刪除目錄到文件系統,因爲junit.xml文件將regardles s的任何變化。

0
date = "dateX" 
    countries = [ "ireland", "wales", "england"] 

    for country in countries: 
     path = "\myhome\%(country)s\modules\builds\%(date)s\junit.xml" \ 
% {"country" : country, "date": date} 
     # check to see if the file you want is there? 
     if os.path.exists(path): 
      root = ET.parse(path).getroot(); 

此外,「os」模塊有一個名爲「walk」的功能,它允許您遍歷整個目錄子樹。你可能想看看你想要「發現」所有名爲junit.xml的文件並處理它們。

2
countries = ['england','wales','germany','etc'] 
countrypath = '\myhome\{}\modules\builds' 
filename = 'junit.xml' 
for country in countries: 
    path = countrypath.format(country) 
    for item in os.listdir(countrypath): 
     if os.path.isdir(item) and item.startswith('date'): 
      os.path.join(path, item, filename)