php binascii.crc32,指定文件生成crc32码写入到指定后缀文件里

#生成CRC32校验码,作为资源版本号,在资源配置json文件写入

from ctypes import *

import binascii

def getFileCRC(_path):

try:

blocksize = 1024 * 64

f = open(_path,"rb")

str = f.read(blocksize)

crc = 0

while len(str) != 0:

crc = binascii.crc32(str, crc)

str = f.read(blocksize)

f.close()

except:

print("get file crc error!")

return 0

return "%08X" % (crc & 0xFFFFFFFF)

import sys

import os

import json

import re

import tkinter as tk

from tkinter import filedialog

root = tk.Tk()

root.withdraw()

#方式1:选中文件中的指定后缀文件

#Filepath = filedialog.askopenfilenames(title='选择资源配置json文件,可多选', filetypes=[('Json', '*.json')]) #获得选择好的文件

path = "E:/Xampp/htdocs/resource"

Filepath=[]

#方式2:指定目录下的指定后缀文件

datanames = os.listdir(path)

for dataname in datanames:

if os.path.splitext(dataname)[1] == '.json':#目录下包含.json的文件

# 将文件绝对路径和文件名拼接

file_path = os.path.join(os.path.abspath(path), dataname)

Filepath.append(file_path)

print(file_path)

fileLen = len(Filepath)

if fileLen == 0:

input('未选择文件\n')

exit(0)

print('选择的文件:')

for i in range(0,fileLen):

print(Filepath[i])

def writefiles():

for i in range(0,fileLen):

filePath = Filepath[i]

with(open(filePath, 'r',encoding='UTF-8')) as fp:

data = json.load(fp)

if 'resources' in data:

resources = data['resources']

if type(resources) == list:

resourceDir = os.path.dirname(filePath)

for j in range(0,len(resources)):

resource = resources[j]

if type(resource) == dict and type(resource['url']) == str:

str_list = re.split(r'\?', resource['url'])

resFilepath = resourceDir + '/' + str_list[0]

resource['url'] = '{0}?v={1}'.format(str_list[0],getFileCRC(resFilepath))

jsonStr = json.dumps(data, ensure_ascii=False,indent = 2)

with(open(filePath, 'w',encoding='UTF-8')) as fp:

fp.write(jsonStr)

print('写入成功',filePath)

writefiles()

print('finished!')

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值