python - 将每个文件与 Python 中另一个文件夹中的相应文件进行比较?

标签 python

此代码采用一个 shapefile,查看其模式并将其与另一个位置的另一个 shapefile(其对应的)进行比较,并打印它们在模式中的差异。

pst_n=fiona.open(r'C:\Users\user\Desktop\new\PST')#new pst
pst_o=fiona.open(r'C:\Users\user\Desktop\old\PST')#old_pst
pst_n.schema
d1 = pst_n.schema['properties']
d2 = pst_o.schema['properties']

d1_items = set(d1.items())
d2_items = set(d2.items())
result = sorted([(k, 'd1', v) for k, v in d1_items if (k, v) not in d2_items] +
                [(k, 'd2', v) for k, v in d2_items if (k, v) not in d1_items])

result = [(k, v, d) for k, d, v in result]


pprint(result)

并显示出这样的差异:

[('ADDRESS', 'int:4', 'd1'),
 ('ADDRESS', 'str:254', 'd2'),
 ('AREA', 'float:19.11', 'd2'),
 ('DEC_ID', 'int:4', 'd1'),
 ('DEC_ID', 'str:254', 'd2'),
 ('DESC_', 'str:254', 'd1'),
 ('FID_PERIVL', 'int:9', 'd1'),
 ('KAEK', 'str:50', 'd1'),
 ('KAEK', 'str:12', 'd2'),
 ('LEN', 'float:19.11', 'd2')

这里是手动完成的。我希望通过在一个主目录中搜索文件的旧版本并将另一个主目录的每个子文件夹中的每个文件与相应的新版本进行比较来完成。

想要的结果是什么

一个主文件夹 A 和 B、C、D... 子文件夹。此主文件夹有正在考虑的文件。大多数子文件夹都有 shps。

不过还有一个主文件夹。我们称它为 K 和 L、M、N.. 子文件夹。这些子文件夹对应于具有 文件的其他文件夹的其他子文件夹。

A 的子文件夹与 K 的子文件夹同名,尽管 K 可能有更多我们不需要的。

我希望用户插入主 A 文件夹的目录并从第一个子文件夹中读取第一个 shp(如果它存在一个 shp)然后转到另一个 old 主文件夹并检查相应的子文件夹,从那里获取 shp 并在它们之间进行一些比较并打印结果(斜体部分我已经解决了)然后相应地继续 new 文件夹的其余子文件夹。如果在一个子文件夹中没有 shp,它应该打印:'folder name' has no shp。并继续剩下的。

初始集合有这段代码:

import fiona
from pprint import pprint
import os
import fnmatch

def new_file_paths(rootdir):
    for dirpath, dirnames, filenames in os.walk(rootdir):
        if dirpath == rootdir: continue. # ignore files in the root
        yield dirpath, [os.path.join(dirpath, fname) for fname in fnmatch.filter(filenames, '*.shp')]

这里采用将比较的两个主要目录:

rootdir_new = r'C:\Users\user\Desktop\a'
rootdir_old = r'C:\Users\user\Desktop\k'

for directory, paths in new_file_paths(rootdir_new)):
    if not paths:
        print('{} is empty, no new files found'.format(directory))
        continue

    for path in paths:
        relative_path = os.path.relpath(path, rootdir_new)
        old_path = os.path.join(rootdir_old, relative_path)
        if not os.path.exists(old_path):
            # no corresponding old file
            print('No matching previous version of {}' 
                  'found, skipping'.format(relative_path))
            continue

        # compare `path` with `old_path`

        d1_items = set(path.items())
        d2_items = set(old_path.items())
        result = sorted([(k, 'd1', v) for k, v in d1_items if (k, v) not in d2_items] +
                        [(k, 'd2', v) for k, v in d2_items if (k, v) not ind1_items])

        result = [(k, v, d) for k, d, v in result]

问题是如何对这两个主目录中具有相同名称的每一对进行实际比较并打印每对的结果?就像开始时一样,但通过循环而不手动打开 shapefile?只是检查它们并打印差异结果。这段代码是否按照文本中的想法执行?我一直计划让它像文本一样工作,但我做不到。

文件在这里用于测试:http://www.mediafire.com/file/644y8e12pj9jrei/main_folders.zip

最佳答案

你只需要结合你的两个部分:

rootdir_new = r'C:\Users\user\Desktop\a'
rootdir_old = r'C:\Users\user\Desktop\k'

for directory, paths in new_file_paths(rootdir_new)):
    if not paths:
        print('{} is empty, no new files found'.format(directory))
        continue

    for path in paths:
        relative_path = os.path.relpath(path, rootdir_new)
        old_path = os.path.join(rootdir_old, relative_path)
        if not os.path.exists(old_path):
            # no corresponding old file
            print('No matching previous version of {}' 
                  'found, skipping'.format(relative_path))
            continue

        # compare `path` with `old_path`

        pst_n=fiona.open(path) #new pst
        pst_o=fiona.open(old_path) #old_pst
        pst_n.schema
        d1 = pst_n.schema['properties']
        d2 = pst_o.schema['properties']

        d1_items = set(d1.items())
        d2_items = set(d2.items())
        result = sorted([(k, path, v) for k, v in d1_items if (k, v) not in d2_items] +
                        [(k, old_path, v) for k, v in d2_items if (k, v) not ind1_items])

        result = [(k, v, d) for k, d, v in result]

关于python - 将每个文件与 Python 中另一个文件夹中的相应文件进行比较?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51057476/

相关文章:

python - Django 通过对象名称注释,这是一个 ForeignKey

python - 使用 numpy.savetxt 同时保持数组的形状

python - 软件中有效的类实例化

python - 将科学记数法转换为 float

python - 将 Twitter 时间转换为特定格式的日期时间,以统计一天中推文的频率

python - 正则表达式 : matching and grouping a variable number of space separated words

python - 投注算法,特别是赢得赌注的算法?

python - Scrapy:不要抓取其他域页面上的链接

具有可变宽度大小的 Python 峰值检测

python - 根据每个像素的位置有效地应用函数