Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

converting man .html file into .txt suing python

I have a directory on my drive and it has many.html files. Those files are text. I have the following code to convert one .html into .txt file.
How can I make iterations for all files and save each file as .txt with its original name?

thank you in advance

from bs4 import BeautifulSoup
markup = open("/content/drive/MyDrive/arc_Articlesww0c5e.html")
soup = BeautifulSoup(markup.read())
markup.close()
f = open("arc_Articlesww0c5e.txt", "w")
f.write(soup.get_text())
f.close()

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

This might give you an idea of how to proceed:

import os
from bs4 import BeautifulSoup

your_dir = "/content/drive/MyDrive"

for file in os.listdir(your_dir):
    if file.endswith((".htm", ".html")):
        with open(os.path.join(your_dir, file)) as markup:
            soup = BeautifulSoup(markup.read())
        with open(file.split(".")[0]+".txt", "w") as f:
            f.write(soup.get_text())
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading