Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8788017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T22:02:54+00:00 2026-06-13T22:02:54+00:00

I’m trying to make an application to create a xml file and I want

  • 0

I’m trying to make an application to create a xml file and I want to assign a text to certain elements. This text consists on image files on a folder. The code is as follows:

    import glob
    import os
    import os.path

    from xml.etree import ElementTree
    from xml.dom import minidom
    import xml.etree.ElementTree as ET

    def prettify(elem):
    """Return a pretty-printed XML string for the Element.
    """
        rough_string = ElementTree.tostring(elem, 'utf-8')
        reparsed = minidom.parseString(rough_string)
        return reparsed.toprettyxml(indent="  ")

    path = "/home/unkuiri/Ubuntu One/Wallpapers/*"


    background = ET.Element('background')
    starttime = ET.SubElement(background, 'starttime')
    year = ET.SubElement(starttime, 'year')
    month = ET.SubElement(starttime, 'month')
    day = ET.SubElement(starttime, 'day')
    hour = ET.SubElement(starttime, 'hour')
    minute = ET.SubElement(starttime, 'minute')
    second = ET.SubElement(starttime, 'second')
    static = ET.SubElement(background, 'static')
    duration_stat = ET.SubElement(static, 'duration')
    files = ET.SubElement(static, 'file')
    transition = ET.SubElement(background, 'transition')
    duration_trans = ET.SubElement(transition, 'duration')
    from1 = ET.SubElement(transition, 'from')
    to = ET.SubElement(transition, 'to')

    dirList = glob.glob(path)

    while len(background.findall("./static/file")) <= len([name for name in os.listdir('.') if os.path.isfile(name)]):
            background.append(static)
            background.append(transition)
            continue

    for fname in dirList:   

        to.text = fname
        files.text = fname
        from1.text = fname


    print prettify(background)

This code outputs a correctly formatted xml but only with the last path, repeating it as many times as the number of files in folder. What I want is for it to print one path per “file” element and that same path on the preceeding “to” element and the next from “element”. Maybe it is a simple solution that I’m not aware of. I’m still a newbie.

Thanks in advance

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T22:02:55+00:00Added an answer on June 13, 2026 at 10:02 pm

    I’ve managed to solve this question with this code:

        import glob
        from xml.etree import ElementTree
        from xml.dom import minidom
    
        def prettify(elem):
            """Return a pretty-printed XML string for the Element.
            """
            rough_string = ElementTree.tostring(elem, 'utf-8')
            reparsed = minidom.parseString(rough_string)
            return reparsed.toprettyxml(indent="  ")
    
        import xml.etree.ElementTree as ET
    
        path = "/home/unkuiri/Ubuntu One/Wallpapers/*"
    
    
        background = ET.Element('background')
        dirList = glob.glob(path)
        starttime = ET.SubElement(background, 'starttime')
        year = ET.SubElement(starttime, 'year')
        year.text = '2012'
        month = ET.SubElement(starttime, 'month')
        month.text = '10'
        day = ET.SubElement(starttime, 'day')
        day.text = '10'
        hour = ET.SubElement(starttime, 'hour')
        hour.text = '00'
        minute = ET.SubElement(starttime, 'minute')
        minute.text = '00'
        second = ET.SubElement(starttime, 'second')
        second.text = '00'
    
    
        for i,fname in enumerate(dirList):    
    
            static = ET.SubElement(background, 'static')
            duration_stat = ET.SubElement(static, 'duration')
            duration_stat.text = '1795.0'
            files = ET.SubElement(static, 'file')
            transition = ET.SubElement(background, 'transition')
            duration_trans = ET.SubElement(transition, 'duration')
            duration_trans.text = '5.0'
            from1 = ET.SubElement(transition, 'from')
            to = ET.SubElement(transition, 'to')
    
            from1.text = dirList[i-1]
    
            files.text = dirList[i-1]
    
            to.text = dirList[i]
    
    
        print prettify(background)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to render a haml file in a javascript response like so:
I want use html5's new tag to play a wav file (currently only supported
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I'm trying to create an if statement in PHP that prevents a single post

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.