我担心文件和目录的顺序os.walk().如果我有这些目录,1,10,11,12,2,20,21,22,3,30,31,32,什么是输出列表的顺序?
它是按数值排序的吗?
1 2 3 10 20 30 11 21 31 12 22 32
Run Code Online (Sandbox Code Playgroud)
或者按ASCII值排序,就像给出的那样ls?
1 10 11 12 2 20 21 22 3 30 31 32
Run Code Online (Sandbox Code Playgroud)
另外,我该如何获得特定的排序?
我想在 python 中为给定目录创建一个唯一的哈希值。感谢 zmo 提供了下面的代码,为目录中的每个文件生成哈希值,但如何聚合这些代码以生成表示该文件夹的单个哈希值?
import os
import hashlib
def sha1OfFile(filepath):
sha = hashlib.sha1()
with open(filepath, 'rb') as f:
while True:
block = f.read(2**10) # Magic number: one-megabyte blocks.
if not block: break
sha.update(block)
return sha.hexdigest()
for (path, dirs, files) in os.walk('.'):
for file in files:
print('{}: {}'.format(os.path.join(path, file),
sha1OfFile(os.path.join(path, file)))
Run Code Online (Sandbox Code Playgroud)