01/10/2018, 16:59

Đọc tất cả file XML có trong thư mục và in ra số lượng Elements có trong XML

Em có 1 thư mục chưa tất cả file XML, em muốn đọc hết các file XML có trong đó rồi in ra số lượng Object có trong file, code của em như thế này nhưng nó lại không hiện ra đúng ý em muốn dù không có bug, mong mọi người giúp em:

Code:

from xml.dom.minidom import parse
import os

base = "/Users/thaihuy/Desktop/xml_File"
for i in os.listdir(base):
    p = os.path.join(base, i)
    try:
        doc = parse(p[0])
    except OSError:
        continue
    obj = doc.getElementsByTagName('object')
    print(len(obj))

1 file XML mẫu :

<annotation>
	<folder>car</folder>
	<filename>Car2 (4).jpeg</filename>
	<path>C:UsersMinhNghiaDownloadsGoogle ImagescarCar2 (4).jpeg</path>
	<source>
		<database>Unknown</database>
	</source>
	<size>
		<width>600</width>
		<height>840</height>
		<depth>3</depth>
	</size>
	<segmented>0</segmented>
	<object>
		<name>car</name>
		<pose>Unspecified</pose>
		<truncated>0</truncated>
		<difficult>0</difficult>
		<bndbox>
			<xmin>10</xmin>
			<ymin>190</ymin>
			<xmax>598</xmax>
			<ymax>427</ymax>
		</bndbox>
	</object>
	<object>
		<name>car</name>
		<pose>Unspecified</pose>
		<truncated>0</truncated>
		<difficult>0</difficult>
		<bndbox>
			<xmin>17</xmin>
			<ymin>551</ymin>
			<xmax>363</xmax>
			<ymax>792</ymax>
		</bndbox>
	</object>
</annotation>
Quân viết 19:00 ngày 01/10/2018

không đúng là như thế nào, đừng bắt người ta phải đoán chứ

Trương Tấn Phát viết 19:01 ngày 01/10/2018
except OSError:
        continue

Có ngoại lệ nhưng vẫn tiếp tục mà không thông báo hay in thông tin ngoại lệ liệu có ổn không?

Bài liên quan
0