Oct-22-2021, 03:50 PM
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
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
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.