为您提供Python情境的新手程序员。使用Python解析文件夹中除了在XML文件中键入的文件之外的所有文件
我有什么:
- 包含,文件夹以及其他文件夹(模块)和文件(可能它是.txt,.C,.H,的.py等)
- 基本上包含该文件夹的配置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:
这是两个事情,它不会做正确的:
- 文件同名的文件会损坏输出。
- 除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)
它是接近的一个好办法吗?
这看起来像一个很好的问题,但我会问长块元评注或恳求被忽略,因为它们通常会被修剪,并会导致某些人做某些工作来做到这一点。从问题的表现出发,努力应该是显而易见的,而不是因为最后有一个长期的免责声明(这些矛盾有时会导致低估,因为有些读者不喜欢辩护)。长话短说:保持简洁! – halfer
@halfer明白了,谢谢。 –