You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
60 lines
1.9 KiB
60 lines
1.9 KiB
import hashlib
|
|
import json
|
|
import os
|
|
|
|
#获取目录下的全部文件列表
|
|
def get_all_filenames(parentPath,folder_path,isRootPath):
|
|
filenames = []
|
|
contents = os.listdir(folder_path)
|
|
for content in contents:
|
|
path = os.path.join(folder_path, content)
|
|
if isRootPath == True:
|
|
path=path.replace("/","\\")
|
|
filenames.append(path)
|
|
else:
|
|
if os.path.isfile(path):
|
|
filePath=os.path.join(folder_path,content)
|
|
filePath=filePath.replace(parentPath+"\\","")
|
|
filePath=filePath.replace("/","\\")
|
|
filenames.append(filePath)
|
|
else:
|
|
subfolder_filenames = get_all_filenames(parentPath,path,isRootPath)
|
|
filenames.extend(subfolder_filenames)
|
|
return filenames
|
|
|
|
#验证某个文件路径是否在文件list列表中
|
|
def hasInList(fn,list):
|
|
for f in list:
|
|
if f in fn:
|
|
return True
|
|
return False
|
|
|
|
#获取某个文件的MD5码
|
|
def getHas(filePath):
|
|
pathArr = filePath.split(".")
|
|
if len(pathArr) > 2 and pathArr[2] != "json":
|
|
return pathArr[2]
|
|
return None
|
|
|
|
# 根据路径创建一个md5字符串
|
|
def create_md5(input_str):
|
|
md5 = hashlib.md5() # 创建一个md5对象
|
|
md5.update(input_str.encode('utf-8')) # 使用utf-8编码
|
|
return md5.hexdigest() # 返回十六进制的哈希值
|
|
|
|
#检查文件路径是否存在,如果不存在就创建一个目录
|
|
def existsDir(path):
|
|
if not os.path.exists(path):
|
|
os.makedirs(path, exist_ok=True)
|
|
# 读取json文件
|
|
def readJson(path):
|
|
if os.path.exists(path):
|
|
f=open(path,encoding="utf-8")
|
|
jsonobj = json.load(f)
|
|
f.close();
|
|
return jsonobj
|
|
# 写json数据到本地
|
|
def writeJson(path,content):
|
|
f=open(path,"w")
|
|
json.dump(content,f)
|
|
f.close()
|