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

2024-04-20 03:18:35 发布

您现在位置:Python中文网/ 问答频道 /正文

这段代码获取一个shapefile,查看它的模式,并将其与另一个位置的另一个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。。。子文件夹。此主文件夹有新的正在考虑中的文件。大多数子文件夹都有SHP。你知道吗

不过,还有一个主文件夹。我们用L,M,N来称呼它。。子文件夹。这些子文件夹是另一个文件夹的其他子文件夹与新文件相对应的子文件夹。你知道吗

A中的子文件夹与K中的子文件夹具有相同的名称,尽管K中可能有更多我们不需要的子文件夹。你知道吗

我希望用户插入主A文件夹的目录并从第一个子文件夹中读取第一个shp(如果存在shp),然后转到另一个old主文件夹并检查相应的子文件夹,从那里得到shp,并在它们之间做一些比较,然后打印结果(斜体部分我已经解决了),然后继续new文件夹的其他子文件夹因此,如果在一个子文件夹中,没有应该打印的shp:“文件夹名”没有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


Tags: pathin文件夹newforifnotitems
1条回答
网友
1楼 · 发布于 2024-04-20 03:18:35

你只需要把两部分结合起来:

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]

相关问题 更多 >