Reputation: 578
I need a simple python script to look in a folder get the image filenames which there 100 in each folder and output and xml as such:
<image src = "img_3.jpg"/>
<image src = "img_4.jpg"/>
<image src = "img_5.jpg"/>
<image src = "img_6.jpg"/>
<image src = "img_7.jpg"/>
<image src = "img_8.jpg"/>
<image src = "img_9.jpg"/>
<image src = "img_10.jpg"/>
<image src = "img_11.jpg"/>
<image src = "img_12.jpg"/>
<image src = "img_13.jpg"/>
<image src = "img_14.jpg"/>
where the img_X.jpg
is the image name from inside the folder. my images are named 851_r016_OUTPUT_JPG_001
, 851_r016_OUTPUT_JPG_002
and so forth. I know its possible just don't know how to make it happen.
Just to clarify I need help with a python script that will search in a specified windows folder grab all the image names in that folder and output an xml with the above format.
Upvotes: 0
Views: 4646
Reputation: 508
You can use PyXB and define an 'MyImages' XSD Object, and it will auto generate the code for you - the class and it's methods, including "append", "read"(xml file) and "toxml"(write).
XSD schema:
<xsd:schema xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<xsd:complexType name="MyImages">
<xsd:sequence>
<xsd:complexType name="image">
<xsd:sequence>
<xsd:element name="src" type="xsd:string"/>
</xsd:sequence>
</xsd:complexType>
</xsd:sequence>
</xsd:complexType>
The next step will be simple code, for example:
from os import listdir
from MyImages import MyImages # auto-generated .py file
myimages = MyImages()
for imagePath in listdir('dirPath'):
im = MyImages.image()
im.src = imagePath
myimages.append(im)
with open("output.xml", 'w') as f:
f.write(myimages.toxml("utf-8"))
Upvotes: 2
Reputation: 5210
Use this as a starting point:
import os
for i in os.listdir('your/directory/'):
print(i)
Upvotes: 1