2015-12-21 55 views
0

目前我得到以下错误替代cmp从python 2移动到3?小蟒蛇的经验。

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是由蟒蛇3下降,我尝试了建议(A> B) - (一< b)由通过什么之后代入(前>)在Python 3的新功能 - (<之后),但我得到以下几点:

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

如果您想知道文件是否已添加到(或已从目录中删除),请比较dict所具有的keys的数量。
根据你的算法,当找到一个新文件时,应该添加一个新的密钥(这意味着当删除一个文件时,你会发现比以前少的密钥)。

if (len(before.keys()) < len(after.keys())): 
     break; 
print('New file detected.\n')  
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 
对它们进行比较