Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
md5 = MD5CryptoServiceProvider() лучше бы вынести в глобальную переменную, чтобы не создавать новый объект на каждой итерации. 9| result = StringBuilder()
10| for b in hash:
11| result.Append(b.ToString("x2"))
12| return result.ToString()
output += fname.replace(rootpath, '', 1) + ':' + md5sum + '\n'output += Path.GetFileName(fname) + ':' + md5sum + '\n'Environment.NewLinedef getMD5sum(fileName):
try:
b = System.IO.File.ReadAllBytes(fileName)
except System.IO.FileNotFoundException:
print 'file ' + fileName + ' deleted'
result = ''
except System.IO.IOException:
print 'file ' + fileName + ' is in use'
result = ''
else:
hash = md5.ComputeHash(b)
hashStr = StringBuilder()
for b in hash:
hashStr.Append(b.ToString("x2"))
result = hashStr.ToString()
return result
def getMD5sum(fileName):
return hashlib.md5(open(fileName, 'rb').read()).hexdigest()output+='{0}:{1}\n'.format(fname[len(rootpath):], md5sum)output+='{0}:{1}\n'.format(fname[len(rootpath):], md5sum)Еще мне не нравится, что вы используете os.walk(), который разбивает имя файла на части, а потом назад его собираете. Но я не нашел другого способа рекурсивно получить все файлы.
return zlib.crc32(open(fileName, 'rb').read())def getMD5sum(fileName):
return hashlib.md5(fileName).hexdigest()
from System.IO import StreamWriter, Directory, SearchOption, File, Path
from System import String, BitConverter, Environment, Array
from System.Security.Cryptography import MD5CryptoServiceProvider
from System.Diagnostics import Stopwatch
sw = Stopwatch.StartNew()
def getMD5sum(fileName):
stm = File.OpenRead(fileName)
md5 = MD5CryptoServiceProvider()
hash = md5.ComputeHash(stm)
stm.Close()
return BitConverter.ToString(hash).Replace("-", "").ToLower()
rootpath = 'app'
workingDir = Environment.CurrentDirectory
#hack to get rid of string replace in output
Environment.CurrentDirectory = rootpath
appFiles = Directory.EnumerateFiles('.', '*', SearchOption.AllDirectories)
output = StreamWriter(File.OpenWrite(Path.Combine(workingDir, 'checksums-fastest.csv')))
#magically enumerate some how speedup loop, probably .net -> python iterators interop flavor
for _, file in enumerate(appFiles):
output.Write(file)
output.Write(":")
output.WriteLine(getMD5sum(file))
output.Close()
Environment.CurrentDirectory = workingDir
print sw.Elapsed
CPython vs. IronPython: вычисление MD5-хеша