0

I have stored about 150+ XML files in one folder. I want to open and read those XML files from that folder (about 150+ XML files); after that, I do the next analysis. What do I need to change in the below code to open/read the multiple XML files from that folder?

from bs4 import BeautifulSoup
import lxml
import pandas as pd 

infile = open("F:\\itprocess\\xmltest.xml","r")
contents = infile.read()
jwodder
  • 54,758
  • 12
  • 108
  • 124
  • You can use `os.listdir()` or `glob.glob()` to find all names in a directory. – AKX Sep 09 '18 at 18:57
  • @AKX can you please give me an example? –  Sep 09 '18 at 19:02
  • You can literally Google for "stackoverflow glob.glob" to find this. https://stackoverflow.com/questions/3964681/find-all-files-in-a-directory-with-extension-txt-in-python/3964691#3964691 – AKX Sep 09 '18 at 19:09

1 Answers1

0

os module's listdir() function is a good way to use while reading multiple files.

from bs4 import BeautifulSoup
import lxml
import pandas as pd 
import os    

d = os.listdir()
for file in d:
    infile = open(file,"r")
    contents = infile.read()

Of course here I am assuming you only have your XML files in your current directory.

teoman
  • 959
  • 2
  • 10
  • 21