この関数を使用して、 159251-TreeOfFunFiles のファイルに SHA1 署名を作成しています
def createFileSignature (filename):
"""CreateFileHash (file): create a signature for the specified file
Returns a tuple containing three values:
(the pathname of the file, its last modification time, SHA1 hash)
"""
f = None
signature = None
try:
filesize = os.path.getsize(filename)
modTime = int(os.path.getmtime(filename))
f = open(filename, "rb") # open for reading in binary mode
hash = hashlib.sha1()
s = f.read(16384)
while (s):
hash.update(s)
s = f.read(16384)
hashValue = hash.hexdigest()
signature = (filename, modTime, hashValue)
except IOError:
signature = None
except OSError:
signature = None
finally:
if f:
f.close()
return (signature)
そしてos.path
、Python のモジュールを使用して各ファイルをループします。
signatures = {}
for (dirpath, dirnames, filenames) in os.walk(directory):
for f in filenames:
file_path = dirpath + "\\" + f;
if os.path.exists(file_path):
signatures[file_path] = sha.createFileSignature(file_path) #create a signature from the file
{}
これは、署名の辞書を作成しているだけです。
for key in signatures:
if signatures[key] is not None:
print signatures[key] #(doesnt really check against anything yet)
そして、これは を出力 します。私の質問は、この辞書からまたは要素('c:\\treeoffunfiles\\README.md', 1349709960, 'f430dc83251684703072a55eee0b0b6a2417c5e4')
を取得するにはどうすればよいですか? 試してみましたが、運がありません2nd
3rd
print signatures[key][1]