批量获取文件的摘要(md5、sha1、sha256)升级V-3
0x01 功能
在上次的基础上,又根据同事的要求,对小工具进行了新的升级,升级的功能包括:
1.针对文件夹中嵌套文件夹的文件有效;
2.可以使用绝对路径;
3.去除了相对路径的功能;
4.添加了文件重命名的功能;
5.通过pyinstaller生成了可执行文件
0x02 过程简述
1、python遍历文件夹
方式1 os.listdir(path)
import os
def func(path):
for i in os.listdir(path):
path2 = os.path.join(path, i) # 拼接绝对路径
if os.path.isdir(path2): # 判断如果是文件夹,调用本身
func(path2)
else:
print(i) # 输出文件夹下所有文件名
print(path2) # 输出文件夹下所有文件的绝对路径
func('e:\\temp')
方式2 os.listdir(path)
import os
def func(path):
if os.path.isdir(path):
# 获得的路径为文件夹
for root, dirs, files in os.walk(path):
print(root) # 当前根目录
print(dirs) # 子文件夹名
for file in files:
print(file) # 子文件
func('e:\\temp')
2、生成可执行文件
使用的工具是:pyinstaller
python版本是:python38
可能出现不兼容的问题:pyinstaller建议最新开发版:
pip instal https://github.com/pyinstaller/pyinstaller/archive/develop.tar.gz
关于pyinstaller的使用后续更新。
0x03 源代码
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time : 2019/12/29/029 0:46
# @Author : H
# @File : getHash-v2.py
import os
import hashlib
def getHash(filename, filepath, outputpath):
datas = {} # 结果存为json,以便后需
f = open(filepath, "rb")
rb = f.read()
data = {'MD5': hashlib.md5(rb).hexdigest(),
'SHA1': hashlib.sha1(rb).hexdigest(),
'SHA256': hashlib.sha256(rb).hexdigest()}
newname = data['MD5'] + '-' + filename
newname = filepath.replace(filename,newname)
datas[filename] = data
f.close()
# shutil.copyfile(filepath, newname) #重命名并保存原文件
os.rename(filepath,newname) # 直接重命名
try:
with open(outputpath , "a", encoding="utf-8")as f:
f.writelines("filename:\t" + filename + "\n")
print("[+]succeed get the teatures of " + filename)
for key, value in datas[filename].items():
f.writelines(key + ":\t" + value + "\n")
f.writelines("\n")
return True
except Exception as err:
print("Outputpath error")
return False
pass
def func(path,sub):
if os.path.isdir(path):
# 如果绝对路径下的文件夹
for i in os.listdir(path): # i文件名
path2 = os.path.join(path, i) # 拼接绝对路径
if os.path.isdir(path2): # 判断如果是文件夹,调用本身
func(path2,sub)
else:
sub.append(path2)
elif os.path.isfile(path):
# 如果绝对路径下的文件
sub.append(path)
else:
print("File or path doesn\'t exit")
print("""
///
// 888 888 d8888 d8888b 888 888 //
// 888 888 d88888 d88P Y88b 888 888 //
// 888 888 d88P888 Y88b 888 888 //
// 8888888888 d88P 888 Y888b 8888888888 //
// 888 888 d88P 888 Y88b 888 888 //
// 888 888 d88P 888 888 888 888 //
// 888 888 d8888888888 Y88b d88P 888 888 //
// 888 888 d88P 888 Y8888P 888 888 //
///
By H @2019/12/29
""")
flag = True
while flag != '#':
path = input('Enter the filename or foldername under absolute path:')
if path.strip('\n') == '':
continue
else:
path = path.strip('\n')
outputpath = input('Enter outputpath or N/n(Default path: d:\hash.txt):')
if outputpath.strip('\n') == '':
continue
# 输入路径处理
if "/" in path:
path = path.replace("/","\\")
if "/" in outputpath:
outputpath = outputpath.replace("/", "\\")
sub = []
outputpath = outputpath.strip('\n')
if (outputpath != 'N' or outputpath != 'n'):
outputpath = outputpath
if outputpath == 'N' or outputpath == 'n':
outputpath = r'd:\hash.txt'
print(outputpath)
flag1 = False
func(path, sub)
for i in sub:
filename = i.split('\\')[-1]
filepath = i
flag1 = getHash(filename, filepath, outputpath)
if flag1:
print("Outputpath:===>" + outputpath)
flag1 = False
path = ''
outputpath = ''
sub = []
flag = input('Enter \'#\' to quit or others to continue!')
0x04 结果
生成的结果:
运行结果:
生成的hash:
0x05 总结
由于本科期间,没有乖乖写代码,代码量少的可怜,所以写的代码很low很low,写点东西来督促自己;
所亏欠的,迟早是要还的;
今天本命年第一天,我在北京,祝自己生日快乐。