Open and read multiple xml files from the folder
Open and read multiple xml files from the folder
the below holder have 100+ XML files. I have to open and read all those files.
F:ProcessProcess_filesxmls
So far, I did the below code to open single XML file from the folder. What I need to change to open/read all the XML files from the folder.
from bs4 import BeautifulSoup
import lxml
import pandas as pd
infile = open("F:\Process\Process_files\xmls\ABC123.xml","r")
contents = infile.read()
soup = BeautifulSoup(contents,'html.parser')
2 Answers
2
Use the glob and the os module to iterate over every file in a given path with a given file extension:
glob
os
path
import glob
import os
path = "F:/Process/Process_files/xmls/"
for filename in glob.glob(os.path.join(path, "*.xml")):
with open(filename) as open_file:
content = open_file.read()
soup = BeautifulSoup(content, "html.parser")
Tip: Use the with statement so the file gets automatically closed at the end.
with
Source: Open Every File In A Folder
So you need to iterate over files in the folder? You can try something like this:
for file in os.listdir(path):
filepath = os.path.join(path, file)
with open(filepath) as fp:
contents = fp.read()
soup = BeautifulSoup(contents, 'html.parser')
Thanks for contributing an answer to Stack Overflow!
But avoid …
To learn more, see our tips on writing great answers.
Required, but never shown
Required, but never shown
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.