目前我收到以下错误
Traceback (most recent call last):
File "C:\Users\Dilshad\Desktop\project_7-8-2015\8_bands\Program_camera.py", line 47, in <module>
if (cmp(before, after) != 0):
NameError: name 'cmp' is not defined
我跑的时候:
# CAMERA IMAGE ANALYZE
camera_directory = "/Users/Dilshad/Dropbox/Camera Uploads" # Directory of camera uploads
os.chdir(camera_directory) # Change directory to location of photo uploads
path = "."
before = dict([(f, None) for f in os.listdir(path)])
print('Waiting for image to be uploaded...\n')
while True: # Wait a new image in the directory
import time
time.sleep(2)
after = dict([(f, None) for f in os.listdir(path)])
if (cmp(before, after) != 0):
break;
print('New file detected.\n') # New image detected
我知道cmp是从python 3中删除的,我尝试通过替换in(之前&gt;之后)从Python 3中的What's New中推荐(a&gt; b) - (a&lt; b) - (之前&之后),但我得到以下内容:
Traceback (most recent call last):
File "C:\Users\Dilshad\Desktop\project_7-8-2015\8_bands\Program_camera - test.py", line 46, in <module>
if ((before > after) - (before < after) != 0):
TypeError: unorderable types: dict() > dict()
关于如何完成这种比较的任何想法?
答案 0 :(得分:0)
如果您想知道文件是否已添加到目录(或从目录中移除),请比较keys
所拥有的dict
数量。
根据你的算法,当找到一个新文件时,应该添加一个新密钥(这也意味着你可以找到比删除文件时更少的密钥)。
if (len(before.keys()) < len(after.keys())):
break;
print('New file detected.\n')
答案 1 :(得分:0)
您只需将它们与==
a = {'a':1, 'b':2}
a = {'a':1, 'b':2}
b = {'a':1, 'b':2}
print(a == b)
b['a'] = 3
print(a == b)
# reset
b['a'] = 1
print(a == b)
# add a new key-value pair
b['c'] = 3
print(a == b)
打印
True
False
True
False