37,720
社区成员
发帖
与我相关
我的任务
分享
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()
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
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