Python 比较 XML.Etree 和目录列表中的文件和文件夹名称的差异

Python compare differences in XML.Etree and file and folder names in directory listings

我正在使用 Python 将我的 flickr 照片与本地硬盘照片目录进行比较。

为了做到这一点,我在 Python 中使用 OAuth 并获取我在 flickr 中的每个 folder/album 的 etree 列表。 flickr 'should' 上的 folder/album 内容与我的本地副本目录匹配。

我希望我的脚本在本地驱动器和 flickr 上的照片列表中没有项目时告诉我(反之亦然)。

flickr 照片的 'title' 字段应与 Linux 上的文件名相同,Linux will/should 上的目录名与 flickr 上的相册名称相匹配。这就是我目前的设置方式。

我想知道在 Python(etree 节点项目与 os.listdir() 项目)中比较这些项目列表的最佳和最有效的方法是什么?

除非必要,否则我宁愿不使用 bash 中的 sort() 将任何管道输出排序为文件名。如果可能的话,我想将所有内容都保留在 Python 中,因为我只是在学习它。

我可以使用 os.listdir() 并将其与返回到 flickr 的 XML.Etree 节点进行比较,但是进行这种比较的最佳方法是什么?

请记住,在比较来自 flickr 和 Linux.

的项目时,列表可能不相同并且可能不会排序

我编写了以下代码片段以从 flickr 获取结果:

...oauth code above...
sets = flickr.photosets.getList(user_id=user_id)
print ("Total sets: " + sets.find('photosets').attrib['total'])
all_sets = sets.find('photosets').findall('photoset')

for each_set in all_sets:
   for node in each_set.findall('title'):
      print ("photoset: " + each_set.get('id') + ", " + node.text + ", photos: ", each_set.get('photos'))
      all_photos = flickr.photosets.getPhotos(user_id=user_id, photoset_id=each_set.get('id'))
      photos = all_photos.find('photoset')
      for photo in photos:
         print (photo.get('title'))

上述代码的输出示例为:

photoset: 72157659163323894, Birthday Party - Nov 21, 2015, photos:  131
...
2015:11:21-16:11:14-IMG_20151121_161114372
2015:11:21-16:11:10-IMG_20151121_161109739
2015:11:21-16:10:36-IMG_20151121_161035497
2015:11:21-15:47:14-IMG_20151121_154713671
2015:11:21-15:43:17-IMG_20151121_154317180
2015:11:21-15:43:15-IMG_20151121_154315539
2015:11:21-15:23:42-IMG_20151121_152342348
2015:11:21-15:23:11-IMG_20151121_152311411
...
2015:11:21-16:21:19-DSC_0603
2015:11:21-16:21:13-DSC_0602
2015:11:21-16:21:11-DSC_0601
2015:11:21-16:21:09-DSC_0600
2015:11:21-16:21:07-DSC_0599
2015:11:21-16:21:05-DSC_0598
2015:11:21-16:20:13-DSC_0597
2015:11:21-16:20:09-DSC_0596
2015:11:21-16:19:59-DSC_0595
2015:11:21-16:19:56-DSC_0594
2015:11:21-16:19:55-DSC_0593
...

getPhotos 的 API 在这里:https://www.flickr.com/services/api/flickr.photosets.getPhotos.htm 显示了一些示例 xtree/XML 输出。

Etree API: https://docs.python.org/2/library/xml.etree.elementtree.html

检查您的 flickr 中的文件是否存在于您的硬盘上:

not_on_hd = []
for file in flickr_photos:
    if os.path.exists("path/to/"+file):
        continue
    else:
        not_on_hd.append(file)
print(not_on_hd)

要反过来做,我会使用一个简单的 if file_on_drive is in flickr_photos,并将 return false 的那些附加到列表中,就像上面一样。

not_on_flickr = []
for file_on_drive in files_on_drive:
    if file_on_drive in flickr_photos:
        continue
    else:
        not_on_flickr.append(file_on_drive)
print(not_on_flickr)

由于您要求效率:pop() 在列表的第一个 运行 中找到的任何文件,使第二个 运行 更短。

not_on_hd = []
for i,file in enumerate(flickr_photos):
    if os.path.exists("/path/to/"+file):
        continue
    else:
        not_on_hd.append(file)
        flickr_photos.pop(i)
print(not_on_hd)

这是我在上面所做的一些文档:
enumerate() - python3 docs
is in - Python3 Docs(Section 6.10.2) (And the difference between is and == here)

鸟瞰图:

  1. 从 XML.
  2. 创建一组完整路径名(数据类型 set!)
  3. 从您的本地文件系统创建另一组完整路径名。
  4. 使用 set 操作来获取两侧缺失的路径。