python如何遍历文件夹然后生成md5

wangcx_cxdn 2010-02-05 03:54:15
用python遍历一个文件夹下的所有文件,然后再生成每个文件的md5保存成xml,急呀,求求各位了?必要的地方给点注释
...全文
537 9 打赏 收藏 转发到动态 举报
写回复
用AI写文章
9 条回复
切换为时间正序
请发表友善的回复…
发表回复
zfzaizheli 2010-02-09
  • 打赏
  • 举报
回复
学习了~~~~~~~~~~~
wangcx_cxdn 2010-02-08
  • 打赏
  • 举报
回复
1,2,3楼的正确,我先前没有仔细看代码,仔细一看是对的,谢谢啦
thy38 2010-02-06
  • 打赏
  • 举报
回复
[Quote=引用 3 楼 angelipin 的回复:]
我无聊死了。。。。。。。
[/Quote]比较可爱的说~
notax 2010-02-06
  • 打赏
  • 举报
回复
在简单点(在linux上)

find /your/path -name '*' -exec md5sum {} \; > all_md5.txt
angelipin 2010-02-05
  • 打赏
  • 举报
回复
没有。。。。。
无聊。。。。
wangcx_cxdn 2010-02-05
  • 打赏
  • 举报
回复
有这么麻烦吗?
angelipin 2010-02-05
  • 打赏
  • 举报
回复
我无聊死了。。。。。。。
import os
import hashlib
from xml.dom import minidom,Node
import codecs

xml = ''
def Indent(dom, node, indent = 0):
# Copy child list because it will change soon
children = node.childNodes[:]
# Main node doesn't need to be indented
if indent:
text = dom.createTextNode('\n' + '\t' * indent)
node.parentNode.insertBefore(text, node)
if children:
# Append newline after last child, except for text nodes
if children[-1].nodeType == node.ELEMENT_NODE:
text = dom.createTextNode('\n' + '\t' * indent)
node.appendChild(text)
# Indent children which are elements
for n in children:
if n.nodeType == node.ELEMENT_NODE:
Indent(dom, n, indent + 1)

def insertnewmsg(filename,md5):
if not os.path.exists("FileInfo.xml"):
dom = minidom.parseString("<root></root>")
domcopy = dom.cloneNode(True)
f = file("FileInfo.xml",'wb')
writer = codecs.lookup('utf-8')[3](f)
domcopy.writexml(writer, encoding='utf-8')
domcopy.unlink()
writer.close()
return
dom = minidom.parse('FileInfo.xml')
root = dom.getElementsByTagName('root')
ve = dom.createElement('info')
ve.setAttribute('name',filename)
ve.setAttribute('md5',md5)
root[0].appendChild(ve)
domcopy = dom.cloneNode(True)
f = file("FileInfo.xml",'wb')
writer = codecs.lookup('utf-8')[3](f)
domcopy.writexml(writer, encoding='utf-8')
domcopy.unlink()
writer.close()

return

def walk(path):
global xml
fl = os.listdir(path) # get what we have in the dir.
for f in fl:
if os.path.isdir(os.path.join(path,f)): # if is a dir.
walk(os.path.join(path,f))
else: # if is a file
fp = open(os.path.join(path,f)) #open file. There is a big problem here. That is when you get a large file.
c = fp.read() # get file content.
m = hashlib.md5() # create a md5 object
m.update(c) #encrypt the file
xml += "<md5 name=\"%s\" md5=\"%s\" />\n" % (os.path.join(path,f),str(m.hexdigest())) # output to the md5 value to a string in xml format.
fp.close() #close file
insertnewmsg(os.path.join(path,f),str(m.hexdigest())) #choice 2 write to xml.
if __name__ == "__main__":
xml = ''
insertnewmsg('','')
walk(os.getcwd())
print xml
'''
make the xml file look nice
'''
dom = minidom.parse('FileInfo.xml')
domcopy = dom.cloneNode(True)
Indent(domcopy,domcopy.documentElement)
f = file("FileInfo.xml",'wb')
writer = codecs.lookup('utf-8')[3](f)
domcopy.writexml(writer, encoding='utf-8')
domcopy.unlink()
writer.close()
angelipin 2010-02-05
  • 打赏
  • 举报
回复
import os
import hashlib
xml = ''
def walk(path):
global xml
fl = os.listdir(path) # get what we have in the dir.
for f in fl:
if os.path.isdir(os.path.join(path,f)): # if is a dir.
walk(os.path.join(path,f))
else: # if is a file
fp = open(os.path.join(path,f)) #open file. There is a big problem here. That is when you get a large file.
c = fp.read() # get file content.
m = hashlib.md5() # create a md5 object
m.update(c) #encrypt the file
xml += "<md5 name=\"%s\" md5=\"%s\" />\n" % (os.path.join(path,f),str(m.hexdigest())) # output to the md5 value to a string in xml format.
fp.close() #close file
if __name__ == "__main__":
xml = ''
walk(os.getcwd())
print xml
angelipin 2010-02-05
  • 打赏
  • 举报
回复
import os
import hashlib
xml = ''
def walk(path):
global xml
fl = os.listdir(path)
for f in fl:
if os.path.isdir(os.path.join(path,f)):
walk(os.path.join(path,f))
else:
c = open(os.path.join(path,f)).read()
m = hashlib.md5()
m.update(c)
xml += "<md5 name=\"%s\" md5=\"%s\" />\n" % (os.path.join(path,f),str(m.hexdigest()))
if __name__ == "__main__":
xml = ''
walk(os.getcwd())
print xml

37,720

社区成员

发帖
与我相关
我的任务
社区描述
JavaScript,VBScript,AngleScript,ActionScript,Shell,Perl,Ruby,Lua,Tcl,Scala,MaxScript 等脚本语言交流。
社区管理员
  • 脚本语言(Perl/Python)社区
  • IT.BOB
加入社区
  • 近7日
  • 近30日
  • 至今

试试用AI创作助手写篇文章吧