the below holder have 100+ XML files. I have to open and read all those files.
F:\\Process\\Process_files\\xmls
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')
Use the glob
and the os
module to iterate over every file in a given path
with a given file extension:
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.
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')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.