Simnicjon
Simnicjon

Reputation: 105

Apply Beautiful Soup to multiple txt files

Grateful for your help. I'm trying to apply Beautiful Soup to all text files in a directory. The code below only applies it to one file. Could someone please help me figure out what I'm doing wrong. Thanks!

from bs4 import BeautifulSoup
import os
import glob

path = '/Scripts/Demo_Contents/'

for infile in glob.glob(os.path.join(path, "*.txt")):
  with open(infile, "rb") as f:
   text = (f.read())

  cleantext = BeautifulSoup(text, 'lxml').text

with open(infile, 'w') as myfile:
  myfile.write(cleantext)

Upvotes: 0

Views: 109

Answers (1)

wasif
wasif

Reputation: 15488

Fix your indentation for this:

from bs4 import BeautifulSoup
import os
import glob

path = '/Scripts/Demo_Contents/'

for infile in glob.glob(os.path.join(path, "*.txt")):
  with open(infile, "rb") as f:
     text = (f.read())
     cleantext = BeautifulSoup(text, 'lxml').text
  with open(infile, 'w') as myfile:
     myfile.write(cleantext)

Upvotes: 1

Related Questions