Python 从多个位置读取同一文件名

Python 从多个位置读取同一文件名,python,string,Python,String,我的脚本所在的目录中有一个名为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 一切都很好 但是,我有

我的脚本所在的目录中有一个名为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
一切都很好

但是,我有一个更复杂的示例,其中我需要解析一连串连续在不同目录中的同名文件“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
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>
现在,每个目录都有XML文件的集合。我只想将所有名为junit.xml的文件放在以下位置:

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

如何以Python的方式实现这一点,在需要更改国家名称和日期的情况下?

使用字符串模板作为路径,例如:

path = r"\myhome\{}\modules\builds\date{}"
您以后可以使用该函数(例如,
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文件的路径并对其进行解析。

首先,定义文件将遵循的“模板”,然后定义国家列表和日期列表:

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()
        # ...

效率不如事先有一个候选目录列表,但您也可以使用
os.walk
递归查找
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
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>
导入操作系统
def get_junit_文件名(目录):
对于os.walk(目录)中的dirpath、dirname和文件名:
如果文件名中有“junit.xml”:
生成os.path.join(dirpath,'junit.xml')
对于get_junit_filenames('/myhome')中的文件名:
这样,您就不必担心在文件系统中添加/删除目录,因为无论发生什么更改,
junit.xml
文件都将被删除

    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的文件并对其进行处理

我认为目录应该是
directory=“\\myhome\\{0}\\modules\\builds\\date{1}”
@RanRag,因为Python2.7(以及Python3.x)可以隐式引用位置参数,也就是说,如果使用
{}
则取
str.format
的下一个参数。这在Python中有解释。好的,我使用的是Python 2.6,这就是为什么我必须使用我的目录版本。你不认为使用rawstrings(
r'…')
)看起来比使用太多斜杠(\\)要好吗?@julio.alegria,你说得对,我已经更新了答案。谢谢
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)