我最终找到了上述问题的答案。下面的代码除了获取文件头外,还做了很多工作。它还同时加载两个并行列表数组,分别带有格式化的文件名数据(带扩展名)和纯头名称数据,这样我就可以使用这些列表在while循环中一次性填充这些html文件中的和格式化的文件名扩展名。代码现在运行良好,如下所示。在def splitHeaderstoFiles(dir, inpath):
count = 1
t_count = 0
out_path = ''
header = ''
write_bodytext = False
file_path_names = []
pure_header_names = []
inpath = dir + os.sep + inpath
with open(inpath, 'rt', encoding=('utf-8')) as infp:
for line in infp:
if '
#strip html tags, convert to start caps
p = re.compile(r'<.>')
header = p.sub('', line)
header = capwords(header)
line_save = header
# Add 0 for count below 10
if count < 10:
header = '0' + str(count) + '_