Python Forum
Rename Files based on XML file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Rename Files based on XML file
#1
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.
Reply


Messages In This Thread
Rename Files based on XML file - by klturi421 - Oct-22-2021, 03:50 PM
RE: Rename Files based on XML file - by ndc85430 - Oct-22-2021, 04:12 PM
RE: Rename Files based on XML file - by snippsat - Oct-22-2021, 05:42 PM
RE: Rename Files based on XML file - by klturi421 - Oct-22-2021, 07:37 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Copy Paste excel files based on the first letters of the file name Viento 2 455 Feb-07-2024, 12:24 PM
Last Post: Viento
  Rename first row in a CSV file James_S 3 591 Dec-17-2023, 05:20 AM
Last Post: James_S
  Move Files based on partial Match mohamedsalih12 2 833 Sep-20-2023, 07:38 PM
Last Post: snippsat
  Rename files in a folder named using windows explorer hitoxman 3 759 Aug-02-2023, 04:08 PM
Last Post: deanhystad
  Rename all files in a folder hitoxman 9 1,514 Jun-30-2023, 12:19 AM
Last Post: Pedroski55
  Making a question answering chatbot based on the files I upload into python. Joejones 1 1,240 May-19-2023, 03:09 PM
Last Post: deanhystad
  rename file RolanRoll 0 540 May-18-2023, 02:17 PM
Last Post: RolanRoll
  '' FTP '' File upload with a specified string and rename midomarc 1 1,174 Apr-17-2023, 03:04 AM
Last Post: bowlofred
  Split pdf in pypdf based upon file regex standenman 1 2,101 Feb-03-2023, 12:01 PM
Last Post: SpongeB0B
  rename same file names in different directories elnk 0 719 Nov-04-2022, 05:23 PM
Last Post: elnk

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020