Python Forum
Rename Files based on XML file - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Rename Files based on XML file (/thread-35341.html)



Rename Files based on XML file - klturi421 - Oct-22-2021

I am working to rename a list of files that are received daily with an index key which is an XML file. Currently file names are given in an indistinct manner. The existing file name structure is as follows AT1_ID1_TT3_ID6-1624820795.4719.WAV .

I receive thousands of tiles with the same name which is a pain to try and identify which file I am looking for. Ideally, the file name would be renamed something a little more meaningful like `Smith John - 6-10-2021 5555555555.WAV'

The first set of code is the XML structure. I am looking to join the

<all>
  <recording_index recapp_id="" recapp_name="" last_generated="0">
    <day_index date="2021-06-27">
      <recording target_name="10 Digit Dialing" filename="AT1_ID1_TT3_ID6-1624820795.4719.WAV" filesize="60K" date="2021-06-27 15:07" source="Gloria Reyes" destination="555206203"/>
      <recording target_name="10 Digit Dialing" filename="AT1_ID1_TT3_ID6-1624820858.4721.WAV" filesize="129K" date="2021-06-27 15:09" source="Gloria Reyes" destination="555674217"/>
      <recording target_name="10 Digit Dialing" filename="AT1_ID1_TT3_ID6-1624820986.4723.WAV" filesize="33K" date="2021-06-27 15:10" source="Gloria Reyes" destination="555875330"/>
      <recording target_name="10 Digit Dialing" filename="AT1_ID1_TT3_ID6-1624821029.4725.WAV" filesize="22K" date="2021-06-27 15:10" source="Gloria Reyes" destination="969426666"/>
      <recording target_name="10 Digit Dialing" filename="AT1_ID1_TT3_ID6-1624821071.4727.WAV" filesize="6K" date="2021-06-27 15:11" source="Gloria Reyes" destination="553055567"/>
    </day_index>
  </recording_index>
</all>
import xml.etree.ElementTree as ET
from posixpath import join
import wave
import os

tree = ET.parse('./06-27-2021/index.xml')
root = tree.getroot()

for file_name in os.listdir('./06-27-2021'):
    if file_name.endswith(".WAV"):
        for child in root:
            for child in child:
                for child in child:
                    filename = child.items()[1][1]
                    
                    username = child.items()[4][1]
                    calldate = child.items()[3][1]
                    calldate = calldate.replace(":","_")
                    destination = child.items()[5][1]
                    output = join(username, calldate, destination)
                    
                    output = output.replace("/", "_")

                    output = join(output, ".WAV")
                    output = output.replace("/", "")
                    
                    os.rename(file_name, output)
I get the following error:
Error:
PS C:\Users\XXXXX\Desktop\ipitomy_python> & C:/Users/XXXXX/AppData/Local/Programs/Python/Python39/python.exe c:/Users/XXXXX/Desktop/ipitomy_python/06-27-2021/sandbox.py Traceback (most recent call last): File "c:\Users\XXXXX\Desktop\ipitomy_python\06-27-2021\sandbox.py", line 27, in <module> os.rename(file_name, output) FileNotFoundError: [WinError 2] The system cannot find the file specified: 'AT1_ID1_TT3_ID6-1624820795.4719.WAV' -> 'Gloria Reyes_2021-06-27 15_07_7878206203.WAV'
From what I gather the code is searching for 'Gloria Reyes_2021-06-27 15_07_7878206203.WAV' but not finding it, instead, I need it to look for 'AT1_ID1_TT3_ID6-1624820795.4719.WAV' and rename it. I'm sure that I have something swapped but I can't quite figure out what it swapped.


RE: Rename Files based on XML file - ndc85430 - Oct-22-2021

So debug your program - work out which value is wrong (adding extra calls to print will help) and where that's coming from. Check whether you've got the arguments to rename the right way round (see the docs to see what they should be).


RE: Rename Files based on XML file - snippsat - Oct-22-2021

os.listdir() dos not return absolute path,so will get problem(file not found) if not is same folder when try to rename.
A trick can be to add line-8,so it change to this folder:
os.chdir('./06-27-2021')
Or join so using absolute path:
os.rename(os.path.join(path, scr), os.path.join(path, dst))



RE: Rename Files based on XML file - klturi421 - Oct-22-2021

(Oct-22-2021, 05:42 PM)snippsat Wrote: os.listdir() dos not return absolute path,so will get problem(file not found) if not is same folder when try to rename.
A trick can be to add line-8,so it change to this folder:
os.chdir('./06-27-2021')
Or join so using absolute path:
os.rename(os.path.join(path, scr), os.path.join(path, dst))

That's exactly what I was missing. I didn't realize that it was not calling to the right folder to run and made so much more sense once I read your reply. Thank you!