【发布时间】:2013-05-01 13:46:51
【问题描述】:
我想在解析驱动器或文件夹时创建一个字典“file_stats”,其中包含带有文件统计信息的对象。
我使用路径+文件名组合作为这本字典的键
这些对象有一个名为“addScore”的方法。
我的问题是文件名有时包含导致这些错误的“-”等字符:
Error: Yara Rule Check error while checking FILE: C:\file\file-name Traceback (most recent call last):
File "scan.py", line 327, in process_file
addScore(filePath)
File "scan.py", line 393, in addScore
file_stats[filePath].addScore(score)
AttributeError: 'int' object has no attribute 'addScore'
我使用文件名作为字典的键来快速检查文件是否已经在字典中。
我应该摒弃将文件路径用作字典键的想法,还是有一种简单的方法来转义字符串?
file_stats = {}
for root, directories, files in os.walk (drive, onerror=walkError, followlinks=False):
filePath = os.path.join(root,filename)
if not filePath in file_stats:
file_stats[filePath] = FileStats()
file_stats[filePath].addScore(score)
【问题讨论】:
-
感谢您发布错误,但如果您附上完整代码会很好
-
字符串是合法的字典键,无论它是否包含破折号。问题可能出在其他地方。字典的值看起来像一个整数。
-
问题似乎是您正在为键控文件名存储一个
int对象,而不是您编写的具有addScore方法的任何Class的实例。跨度>
标签: python dictionary key filenames