2017-08-22 33 views
1

为您提供Python情境的新手程序员。使用Python解析文件夹中除了在XML文件中键入的文件之外的所有文件

我有什么:

  1. 包含,文件夹以及其他文件夹(模块)和文件(可能它是.txt,.C,.H,的.py等)
  2. 基本上包含该文件夹的配置XML文件(模块名称,短名称,但也排除列表中的那些来自排除列表不能考虑采取。)

我打算做什么:

  • 读取XML文件中的信息,并将其保存在的问题,可以帮助我分析正确
  • 解析从给定的文件夹中,除了那些所有文件被排除

到目前为止我的代码如下所示:

<?xml version="1.0"?> 
<Modules> 
    <Module> 
     <Name>MOD_Test1</Name> 
     <Shortname>1</Shortname> 
     <ExcludeList> 
      <File>HeaderFile.h</File> 
      <File>CFile.c</File> 
     </ExcludeList> 
    </Module> 
    <Module> 
     <Name>MOD_Test2</Name> 
     <Shortname>2</Shortname> 
     <ExcludeList> 
      <File>TextFile.txt</File> 
     </ExcludeList> 
    </Module> 
</Modules> 

这显然XML文件

def GetExceptFiles(ListOfExceptFiles = []): 
    tree = ET.ElementTree(file='Config.xml') 
    Modules = tree.getroot() 
    for Module in Modules: 
     for Attribute in Module: 
      if Attribute.tag=='Name': 
       ModuleName = Attribute.text 
      if Attribute.tag=='Shortname': 
       ModuleShortName = Attribute.text 
      for File in Attribute: 
       ExceptFileName = File.text 
       print ('In module {} we must exclude {}'.format(ModuleName, ExceptFileName)) 
     if ExceptFileName is not None:   
      ListOfExceptFiles.append(ExceptFileName) 

这一个会读XML文件并给我必须排除的文件列表。这样做的工作,但很差。假设两个模块有一个名称完全相同的文件,其中一个被排除,而另一个不是。他们都会被跳过。

def Parse(walk_dir): 
print('walk_dir = ' + walk_dir) 
for root, subdirs, files in os.walk(walk_dir): 
    print('-------------------------------------------------------\nroot = ' + root) 
    for filename in files: 
     with open(os.path.join(root, filename), 'r') as src: 
      Text = src.read() 
      print ('\nFile %s contains: \n' %filename) + Text 

现在解析这是我开始。我知道它没有解析,但是一旦我可以读取文件的内容,我当然也可以做其他事情。

至于去除除外文件部分我所做的只是增加一个IF语句到2 FOR

for filename in files: 
     if filename not in ListOfExceptFile: 
      with open(os.path.join(root, filename), 'r') as src: 

这是两个事情,它不会做正确的:

  1. 文件同名的文件会损坏输出。
  2. 除xml中的文件(对于一个模块)之外,有多个文件会导致只跳过最后一个文件。 (在我的例子HeaderFile.h不会被跳过,CFile.c会)

编辑:@ bracco23的回答让我思考,虽然我还没有与模块名成功地映射多个列表作为键(仍然在这个问题上寻求帮助,如果你能)
这是我有从列表清单的想法开始的:

def ReadConfig(Tuples = []): 
tree = ET.ElementTree(file='Config.xml') 
Modules = tree.getroot() 
for Module in Modules: 
    for Attribute in Module: 
     if Attribute.tag=='Name': 
      ModuleName = Attribute.text 
     for File in Attribute: 
      ExceptFileName = File.text 
      Tuple = (ModuleName, ExceptFileName) 
      Tuples.append(Tuple) 

它是接近的一个好办法吗?

+0

这看起来像一个很好的问题,但我会问长块元评注或恳求被忽略,因为它们通常会被修剪,并会导致某些人做某些工作来做到这一点。从问题的表现出发,努力应该是显而易见的,而不是因为最后有一个长期的免责声明(这些矛盾有时会导致低估,因为有些读者不喜欢辩护)。长话短说:保持简洁! – halfer

+0

@halfer明白了,谢谢。 –

回答

0

的工作是相当不错的,有仅仅是需要解决的问题,解决这些问题的调整的MINR列表:

1)在你的GetExceptFiles(ListOfExceptFiles = [])你的文件在的为末添加到列表超过Attribute。这导致只添加最后一个文件的事实。在移动文件内部的检查时,应将所有排除的文件添加到列表中。 (一对夫妇的标签/空间就足够了)

def GetExceptFiles(ListOfExceptFiles = []): 
    tree = ET.ElementTree(file='Config.xml') 
    Modules = tree.getroot() 
    for Module in Modules: 
     for Attribute in Module: 
      if Attribute.tag=='Name': 
       ModuleName = Attribute.text 
      if Attribute.tag=='Shortname': 
       ModuleShortName = Attribute.text 
      for File in Attribute: 
       ExceptFileName = File.text 
       print ('In module {} we must exclude {}'.format(ModuleName, ExceptFileName)) 
       if ExceptFileName is not None:   
        ListOfExceptFiles.append(ExceptFileName) 

此外,您还假设属性的标签只能是NameShortnameExcludeList。虽然这肯定会是这样,但格式错误的文件会破坏您的解析。考虑检查所有属性的标签属性,并在出现错误时发布错误。

2)我假设具有相同名称的文件实际上是在模块之间共享的相同文件,这些文件在一些模块中被排除,但不是全部。如果是这种情况,那么排除文件列表将丢失有关排除所属模块的信息。考虑将模块名称作为关键字使用列表映射,以便每个模块都可以拥有自己的排除文件列表。

编辑使用方式dict(我主要面向Java这种结构被称为在java中的地图,但是在Python是dict)可能是:

def GetExceptFiles(DictOfExceptFiles = {}): 
    tree = ET.ElementTree(file='Config.xml') 
    Modules = tree.getroot() 
    for Module in Modules: 
     for Attribute in Module: 
      if Attribute.tag=='Name': 
       ModuleName = Attribute.text 
      if Attribute.tag=='Shortname': 
       ModuleShortName = Attribute.text 
      for File in Attribute: 
       ExceptFileName = File.text 
       if(ModuleName not in DictOfExceptFiles) 
        DictOfExceptFiles[ModuleName] = [] 
       DictOfExceptFiles[ModuleName].append(ExceptFileName) 
       print ('In module {} we must exclude {}'.format(ModuleName, ExceptFileName)) 

讲究,这里假设ModuleName已经设置在第一个文件之前,这取决于组件的命令,这是XML不能保证的。为了解决这个问题,我会将名称和短名称从子标记移动到模块的XML属性,如下所示:

<Module name="ModuleName" shortName="short name"> 
    ... 
</Module> 
+0

好的提示,谢谢!虽然我将如何做映射?我一直在使用谷歌搜索,但还没有弄清楚。 –

+0

修改为在字典上显示示例。你可以阅读更多关于字典[这里](https://docs.python.org/2/tutorial/datastructures.html) – bracco23

相关问题