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.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

How do I collapse sections of code in Visual Studio Code for Windows?

ャフサォクコ ケウ,コ,ワ メ,ロスョノ゙,クネ,フムカヤヲニ,エコ゚ツ ウイオン゙ケワサネォキモュキォウイノンコチ゚メヌナイゥフュ,カヒウネェ ネ,ホノケ,ムュキ ッボーミュハ,チ ツス ィ メウイマヤ,゙ウチ ヅ ロ,ォジヌェ ャヌット ェ,マャ,チナエヒネソキツテ トホヲヲミーァ