输出无法写入TXT文件

输出无法写入TXT文件

问题描述:

步骤输出:输出无法写入TXT文件

  • 读取多个.html文件目录
  • 提取HTML的标题

需求: - 发送标题为个人.txt文件

预计:任何建议。理想情况下,我想提取的HTML文件名(“23434.html”)整数并命名文本文件作为“23434.txt”

结果: - 没有在指定的路径创建任何txt文件。 - 没有被写入

for file_name in glob.glob(os.path.join(dir_path, "*.html")): 
    with open(file_name) as html_file: 
     soup=BeautifulSoup(html_file) 
     d=soup.title.get_text() 

     #resultfile=re.findall('\d+', file_name) 

    with open("m"+".txt", "w") as outfile: 
     outfile.write(d) 
     outfile.close 
+0

你的文件' “m.txt”'(_sic _!)在您运行脚本的目录中创建。如果你想在别处创建它,你必须提供完整的路径。 – DyZ

+0

是的,对不起在早些时候发布时错过了那个部分。谢谢。但是,如果我想将resultfile = re.findall('\ d +',file_name)作为outfputfile中的文件名,我该怎么办。它会返回我的连接错误。 – lpt

+0

此代码似乎是完美的,如果你没有看到任何错误在终端上,那么它应该工作 –

for fpath in glob.glob(os.path.join(dir_path, "*.html")): 
    with open(fpath) as html_file: 
     soup = BeautifulSoup(html_file) 
     html_title = soup.title.get_text() 
     html_number = os.path.basename(fpath).rsplit('.',1)[0] 

     with open(html_number + '.txt', 'w') as outfile: 
      outfile.write(html_title) 
+0

谢谢inspectorG4dget。 – lpt