Python使用文件中的内容重命名工作目录中该文件类型的所有文件

Python使用文件中的内容重命名工作目录中该文件类型的所有文件,python,html,Python,Html,最初,我需要帮助读取目录中的文件名。我能找到我的答案,但有更多的问题。我试图在文件夹结构中标识的文件中包含xml。在识别这些文件之后,我需要一种方法来解析xml中的元数据。从xml中取出元数据后,我需要保存一个共享相同名称但具有不同文件扩展名的文件。我想保留文件名,并从xml中包含我想要的两个标志。我希望在重命名时将文件从源文件夹移到新文件夹中。这是最终结果,除了我遇到的一些其他障碍外,还包含了我问题的答案。我感谢人们的反馈,我知道我的问题和最初的要求都不好。这是我第一次在这个网站上发帖。再次感

最初,我需要帮助读取目录中的文件名。我能找到我的答案,但有更多的问题。我试图在文件夹结构中标识的文件中包含xml。在识别这些文件之后,我需要一种方法来解析xml中的元数据。从xml中取出元数据后,我需要保存一个共享相同名称但具有不同文件扩展名的文件。我想保留文件名,并从xml中包含我想要的两个标志。我希望在重命名时将文件从源文件夹移到新文件夹中。这是最终结果,除了我遇到的一些其他障碍外,还包含了我问题的答案。我感谢人们的反馈,我知道我的问题和最初的要求都不好。这是我第一次在这个网站上发帖。再次感谢,希望这能帮助其他人


这是我的第一个Python程序,我想用它做更多的事情。如果我做了一些可以修改以提高效率的事情,我会很感激这些提示或指针。

将其加载到DOM中,并提取所需节点的值。 将该值保存在变量中。关闭要重命名的文件。使用变量中存储的值重命名文件


抱歉,这只是伪代码,但所有步骤在许多地方都有很好的文档记录

没有你的代码和你遇到的问题的细节,你很难回答你的问题。
# Import tools used in code - lxml is not a normal library
# The .whl file will need to be downloaded and added - use PIP to install .whl files
import os, gc, shutil, re, xml.etree.cElementTree as ET
# GC is garbage collector
gc.collect()
from lxml import etree
# Read through directory  pulling in filenames
# This currently needs to be modified to match folder structure - will retool to work from anywhere to anywhere
for fileCDR in os.listdir("Absolute File Path"):
    # Check files looking for .cdr
    if fileCDR.endswith(".cdr"):
        # Parse file into string to pull out XML Flags - Try Exception used was having issues with tools
        # Need to remove one of them as they are both not needed
        try:
                pfile = etree.parse(fileCDR).getroot()
        except:
                pfile = ET.parse(fileCDR).getroot()
        # print (file)
        # Read through xml in pfile variable - looking for start time
        for startTime in pfile.findall('XML Tag startTime'):
            start = startTime.text.replace(':', '-')
        # Read through xml in pfile variable looking for Dialed Digits - originalDestinationId   
        for originalDestinationId in pfile.findall('XML Tag originalDestinationId'):
            number = originalDestinationId.text
        # Read through xml in pfile variable - looking for filename - correlationId    
        for correlationId in pfile.findall('XML Tag correlationId'):
            fileName = correlationId.text
        # Store all the collected variables into one variable to rename .cdr files
        cdrDone = (start + "_" + number + "_" + fileName + ".cdr")
        # Store all the collected variables into one variable to rename .ogg files
        new_fileName = (start + "_" + number + "_" + fileName + ".ogg")
        # Store file name of .cdr file to use in if statement to match files up
        compareFile = fileCDR[:-4]
        # Read through directory pulling in filenames
        for fileOGG in os.listdir("Absolute File Path"):
            # Used to check if filename ends in .ogg
            if fileOGG.endswith(".ogg"):
                # Used to trim file name of .ogg to match .cdr file
                compareFileName = fileOGG[:-4]  # type: string
                # Used to make sure filenames match - .cdr and .ogg
                if compareFileName == compareFile:
                # These all currently needs to be modified to match folder structure - will retool to work from anywhere to anywhere
                    oggFile = os.path.join("Absolute File Path", fileOGG)
                    newOGG = os.path.join("Absolute File Path", new_fileName)
                    shutil.move(oggFile, newOGG)
                    cdrFile = os.path.join("Absolute File Path", fileCDR)
                    newCDR = ("Absolute File Path", cdrDone)
                    shutil.move(cdrFile, newCDR)
                    print (newOGG + " " + newCDR)