Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8718619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T06:35:39+00:00 2026-06-13T06:35:39+00:00

I am new to Python, and with some really great assistance from StackOverflow, I’ve

  • 0

I am new to Python, and with some really great assistance from StackOverflow, I’ve written a program that:

1) Looks in a given directory, and for each file in that directory:

2) Runs a HTML-cleaning program, which:

  • Opens each file with BeautifulSoup
  • Removes blacklisted tags & content
  • Prettifies the remaining content
  • Runs Bleach to remove all non-whitelisted tags & attributes
  • Saves out as a new file

It works very well, except when it hits a certain kind of file content that throws up a bunch of BeautifulSoup errors and aborts the whole thing. I want it to be robust against that, as I won’t have control over what sort of content winds up in this directory.

So, my question is: How can I re-structure the program so that when it errors on one file within the directory, it reports that it was unable to process that file, and then continues to run through the remaining files?

Here is my code so far (with extraneous detail removed):

def clean_dir(directory):
    os.chdir(directory)

    for filename in os.listdir(directory):
    clean_file(filename)

def clean_file(filename):

    tag_black_list = ['iframe', 'script']
    tag_white_list = ['p', 'div']
    attr_white_list = {'*': ['title']}

    with open(filename, 'r') as fhandle: 

        text = BeautifulSoup(fhandle)
        text.encode("utf-8")
        print "Opened "+ filename

        # Step one, with BeautifulSoup: Remove tags in tag_black_list, destroy contents.
        [s.decompose() for s in text(tag_black_list)]
        pretty = (text.prettify())
        print "Prettified"

        # Step two, with Bleach: Remove tags and attributes not in whitelists, leave tag contents.
        cleaned = bleach.clean(pretty, strip="TRUE", attributes=attr_white_list, tags=tag_white_list)

        fout = open("../posts-cleaned/"+filename, "w")
        fout.write(cleaned.encode("utf-8"))
        fout.close()

    print "Saved " + filename +" in /posts-cleaned"

print "Done"

clean_dir("../posts/")  

I looking for any guidance on how to write this so that it will keep running after hitting a parsing/encoding/content/attribute/etc error within the clean_file function.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T06:35:40+00:00Added an answer on June 13, 2026 at 6:35 am

    You can handle the Errors using :try-except-finally

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm very new to Python and trying to write a program that parses some
I'm new to python. I want to extract some text from the CNN website.
I am really new to python, just played around with the scrapy framework that
I'm experiencing some really weird problems in Python when trying to inherit from a
I am new to python programming..can someone suggest some way to do the following:
I'm pretty new to Python programming and would appreciate some help to a problem
I'm pretty new to python (couple weeks into it) and I'm having some trouble
I'm fairly new to Python, so I'm trying my hand at some simple code.
I would need some help in python. I'm new in python. I used to
(I'm developing in Python 3.1, so if there's some shiny new 3.x feature I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.