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 8122233
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T05:46:04+00:00 2026-06-06T05:46:04+00:00

New to programming. I wrote a Python code (with help from stackoverflow) to read

  • 0

New to programming. I wrote a Python code (with help from stackoverflow) to read XML files in a directory and use BeautifulSoup to copy the text for an element and write this to an output directory. Here is my code:

from bs4 import BeautifulSoup
import os, sys

source_folder='/Python27/source_xml'

for article in os.listdir(source_folder):
    soup=BeautifulSoup(open(source_folder+'/'+article))

    #I think this strips any tags that are nested in the sample_tag
    clean_text=soup.sample_tag.get_text("  ",strip=True)

    #This grabs an ID which I used as the output file name
    article_id=soup.article_id.get_text("  ",strip=True)

    with open(article_id,"wb") as f:
        f.write(clean_text.encode("UTF-8"))

The problem is I needed this to run for 100,000 XML files. I started the program last night and to my estimate it took 15 hours to run. Is there a way to, instead of reading and writing one at a time, to maybe read/write 100 or 1000 at a time, for example?

  • 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-06T05:46:06+00:00Added an answer on June 6, 2026 at 5:46 am

    The multiprocessing module is your friend. You can automatically scale the code accross all your cpu’s with a process pool like this:

    from bs4 import BeautifulSoup
    import os, sys
    from multiprocessing import Pool
    
    
    source_folder='/Python27/source_xml'
    
    def extract_text(article):
        soup=BeautifulSoup(open(source_folder+'/'+article))
    
        #I think this strips any tags that are nested in the sample_tag
        clean_text=soup.sample_tag.get_text("  ",strip=True)
    
        #This grabs an ID which I used as the output file name
        article_id=soup.article_id.get_text("  ",strip=True)
    
        with open(article_id,"wb") as f:
                f.write(clean_text.encode("UTF-8"))
    
    def main():
        pool = Pool()
        pool.map(extract_text, os.listdir(source_folder))
    
    if __name__ == '__main__':
        main()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Im new to python programming and need some help with some basic file I/O
I'm new to Python programming so forgive me if my code is not efficient,
So I'm really new to programming, I just started learning Python yesterday and I'm
I'm new to programming, and I'm trying to write a Python function to find
I have long programming background, but am new to Python, and am playing around
I'm new programing in Python, and what I need a little help with this
I'm new to Squeak and Smalltalk but not programming in general(i've some Python experience).
diclaimer: I am new to python but have experience programming in Drupal I'm following
I'm new with python programming and GUI. I search on internet about GUI programming
I am new to computer programming and have some experience programming with python. 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.