Python 循环浏览目录中的文件,并比较最新的两个文件是否存在重复项
我正在编写一个python 2.7脚本,以比较从Python 循环浏览目录中的文件,并比较最新的两个文件是否存在重复项,python,python-2.7,list,recursion,Python,Python 2.7,List,Recursion,我正在编写一个python 2.7脚本,以比较从所有当前文件返回的列表中的最新(2)个文件。这将列出目录中的所有文件名,例如myimage.jpg、myimage1.jpg等。 例如,如果我的列表如下所示: 1 myfile1.jpg 2 myfile2.jpg 3 myfile3.jpg 4 myfile4.jpg 5 myfile5.jpg 6 myfile6.jpg 定义为list=['myfile1.jpg'、'myfile2']等 如果文件6和5的哈希值之间的差异为10,
所有当前文件返回的列表中的最新(2)个文件。这将列出目录中的所有文件名,例如myimage.jpg、myimage1.jpg等。
例如,如果我的列表如下所示:
1 myfile1.jpg
2 myfile2.jpg
3 myfile3.jpg
4 myfile4.jpg
5 myfile5.jpg
6 myfile6.jpg
定义为list=['myfile1.jpg'、'myfile2']
等
如果文件6
和5
的哈希值之间的差异为10,则脚本将不执行任何操作,如果小于5,则会将其删除
我在制定如何以递归方式(从末尾开始)比较列表中的最后两个元素时遇到了问题,直到比较完所有列表项为止-有人能帮忙吗
我目前有:
def purgeDups():
print "purging duplicate images every hour...\n\n"
all_current_files = os.listdir('filllll...in...image...dir...here')
for file in all_current_files:
#check latest file... and compare to second latest file based on timestamp of file, recurse until no more files.
#begin comparing files...if difference between latest all_current_files and second latest is less than 5, delete
latest = imagehash.average_hash(Image.open(<<latestfilefrom2linesabove>>))
secLatest = imagehash.average_hash(Image.open(<<secondlatestfrom2linesabove>>))
compare = latest-secLatest
if int(compare) < 5:
os.remove(<<latestfilefrom2linesabove>>)
os.remove(<<secondlatestfilefrom2linesabove>>)
def purgeDups():
打印“每小时清除重复图像…\n\n”
所有当前文件=os.listdir('fillll…in…image…dir…here')
对于所有\u当前\u文件中的文件:
#检查最新文件。。。和基于文件时间戳的第二个最新文件相比,递归直到不再有文件。
#开始比较文件…如果最新的所有当前文件和第二个最新文件之间的差异小于5,请删除
latest=imagehash.average\u散列(Image.open())
secLatest=imagehash.average\u散列(Image.open())
比较=最新秒最新
如果int(比较)<5:
os.remove()
os.remove()
非常感谢。这里不需要递归。如果要比较所有可能的对,我建议您使用嵌套循环,如下所示:
for f1 in os.listdir('.'):
for f2 in os.listdir('.'):
if f1 == f2 or not os.path.exists(f1) or not os.path.exists(f2): # don't compare the same file, or files that have already been deleted
continue
... # file comparison code here
这里不需要递归。如果要比较所有可能的对,我建议您使用嵌套循环,如下所示:
for f1 in os.listdir('.'):
for f2 in os.listdir('.'):
if f1 == f2 or not os.path.exists(f1) or not os.path.exists(f2): # don't compare the same file, or files that have already been deleted
continue
... # file comparison code here
如何确定最新的文件?这就是我想弄明白的。如何获取交互性质的最后两项。例如,比较5+6,然后5+4,然后4+3,然后3+2,etc@John为什么不使用一个嵌套循环,比较所有可能的循环对呢?除非您想从目录@c中找到上次修改的文件ᴏʟᴅsᴘᴇᴇᴅs方法就足够了。你们中有人能给我一个基于@c的例子吗ᴏʟᴅsᴘᴇᴇᴅ'I’请问您如何确定最新的文件?这就是我想弄清楚的。如何获取交互性质的最后两项。例如,比较5+6,然后5+4,然后4+3,然后3+2,etc@John为什么不使用一个嵌套循环,比较所有可能的循环对呢?除非您想从目录@c中找到上次修改的文件ᴏʟᴅsᴘᴇᴇᴅs方法就足够了。你们中有人能给我一个基于@c的例子吗ᴏʟᴅsᴘᴇᴇᴅ's建议请Ethanks@Coldspeed-只是想知道,line2
上的f2将如何比较剩下的第二个和最后一个文件?或者它只是选择任何可用的文件?f2
迭代所有文件,因此如果f1
是倒数第二个文件,并且它仍然存在,那么它将被比较。因此,if f1==f2…
行表示如果f1和f2存在,比较它们并继续?@John不是这样。如果f1==f2
意味着,如果f1和f2引用同一个文件,那么我希望内部循环跳过当前比较,继续下一个比较。明白了,我想比较散列,所以我假设我必须在os.listdir('.')中f2的之后添加比较逻辑:
正确吗?谢谢@Coldspeed-只是想知道,第2行的f2将如何比较剩下的第二个和最后一个文件?或者它只是选择任何可用的文件?f2
迭代所有文件,因此如果f1
是倒数第二个文件,并且它仍然存在,那么它将被比较。因此,if f1==f2…
行表示如果f1和f2存在,比较它们并继续?@John不是这样。如果f1==f2
意味着,如果f1和f2引用同一个文件,那么我希望内部循环跳过当前比较,继续下一个比较。明白了,我想比较散列,所以我假设我必须在os.listdir('.')中f2的之后添加比较逻辑:
正确吗?