例如,使用这个脚本的PythonCaller从属性“_original_folder”和“_revised_folder”指定的两个文件夹路径中创建三个列表属性(见下文),并将列表添加到输入特性中。注意:这个脚本示例只是描述一种收集文件夹下所有文件大小的可能方法。它可能不是你最终目标的最佳选择。请适当修改。PythonCaller脚本示例类FileSizesComparer(object): def input(self, feature): #收集两个文件夹下所有文件的大小。{}, {} # {<相对文件路径>:& lt; size>} collectFileSizes (originalSizes feature.getAttribute (_original_folder)) collectFileSizes (revisedSizes, feature.getAttribute (_revised_folder)) originalPaths =组(originalSizes.keys ()) revisedPaths =组(revisedSizes.keys())对我来说,路径枚举(originalPaths & revisedPaths): sizeDiff = revisedSizes(路径)——originalSizes(路径)feature.setAttribute (“_unchanged {% d}。setattribute ('_unchanged{%d})setattribute ('_unchanged{%d}。setattribute ('_unchanged{%d}。setattribute ('_added{%d})。setattribute ('_added{%d})对于i,枚举中的路径(originalPaths - revisedPaths): features . setattribute ('_deleted{%d}。setattribute ('_deleted{%d})size' % i, originalSizes[path]) self.pyoutput(feature) # Helper function: Collect sizes of all files under a specified root folder. # Arguments # - pathToSize: dictionary {<relative file path> : <size>} # - absRoot: absolute path of the root folder # - relRoot: relative path of the root folder (empty by default) import os def collectFileSizes(pathToSize, absRoot, relRoot=''): for name in os.listdir(absRoot): absPath = os.path.join(absRoot, name) # absolute path of file or directory relPath = os.path.join(relRoot, name) # relative path of file or directory if not os.path.islink(absPath): if os.path.isfile(absPath): pathToSize[relPath] = os.path.getsize(absPath) else: collectFileSizes(pathToSize, absPath, relPath) # recursive call
"_unchanged{}" list contains the information on files existing under both original and revised folders. The list consists of these four members.
"_added{}" list contains the information (filename and size) on files existing only under revised folder.
"_deleted{}" list contains the information (filename and size) on files existing only under original folder.
Thanks @NathanAt亚搏在线Safe和https://knowledge.亚搏在线safe.com/answers/63136/view.html