Python 混淆代码,批量更改文件名,更改前缀,增加前缀,更改文件MD5值

#! /usr/bin/python

# -*- coding: UTF-8 -*-

import os,sys

import random

import string

import re

import md5

import time

import json

import shutil

import hashlib

import time

import argparse

 

import sys

from random import Random

reload(sys)

sys.setdefaultencoding("utf-8")

 

script_path = os.path.split(os.path.realpath(sys.argv[0]))[0]

add_prefix = ""

old_prefix = ""

new_prefix = ""

ios_src_path = ""

project_file_path = ""

 

ignore_path_text = [".a", ".plist", ".storyboard", ".pch",".DS_",".sh",".md","symbols","Gemfile","codeObfuscation.h","index.html"]

 

#首字母大写

def isNeedIgnore(file_path):

    global ignore_path_text

    for ignore_text in ignore_path_text:

        if file_path.find(ignore_text) != -1:

            return True

    return False

 

def replaceStringInFile(full_path, old_text, new_text):

    with open(full_path, "r") as fileObj:

        all_text = fileObj.read()

        fileObj.close()

 

    all_text = all_text.replace(old_text, new_text)

    with open(full_path, "w") as fileObj:

        fileObj.write(all_text)

        fileObj.close()

 

def renameFileInXcodeProj(old_file_name, new_file_name):

    global project_file_path

    if os.path.exists(project_file_path):

        replaceStringInFile(project_file_path, old_file_name, new_file_name)

 

def renameInAllFile(old_text, new_text):

    global ios_src_path

    for parent, folders, files in os.walk(ios_src_path):

        for file in files:

            full_path = os.path.join(parent, file)

            print "____________  %s  %s  %s" %(full_path,old_text,new_text)

            if ".git" in full_path:

                print "-"

            else:

                replaceStringInFile(full_path, old_text, new_text)

 

def dealWithIos():

    print "开始重命名类名"

    global old_prefix, new_prefix, ios_src_path

    strDict = {}

    prve = ''

    curren = ''

    for parent, folders, files in os.walk(ios_src_path):

        for file in files:

            old_full_path = os.path.join(parent, file)

            

            mixPrefix = 0

            mixAll = 0

            changePrefix = 0

            addNewPrefix = 0

            

            OldName = ""

            

            if len(old_prefix)>1 and len(new_prefix)<4:

                mixPrefix = 1

            

            if len(old_prefix)<4 and len(new_prefix)<4:

                mixAll = 1

            

            if len(old_prefix)>1 and len(new_prefix)>1:

                changePrefix = 1

                

            if len(old_prefix)<4 and len(new_prefix)>1:

                addNewPrefix = 1

            

#            if file.startswith(old_prefix) and not isNeedIgnore(old_full_path):

            if not isNeedIgnore(old_full_path):

                

                new_file_name = file.replace(old_prefix, old_prefix)

                fileNames = new_file_name.split('.')

                print fileNames

                

                if fileNames:

                    OldName = fileNames[0]

                

                if mixPrefix == 1 or mixAll == 1:

                    i = 0

 

                    for key in  strDict:

                        if fileNames and key == fileNames[0]:

                            i = 1

                            curren = strDict[key]

                            prve = key

                    if i:

                        pass

                    else:

                        str = ''

                        chars = 'BbCcDdEeFfGgHhIiJjKkLlMmNnOoQqRrSsTtUuVvXxYyZz'

                        length = len(chars) - 1

                        random = Random()

                        for i in range(8):

                            str += chars[random.randint(0, length)]

                        prve = fileNames[0]

                        curren = str

                        strDict[prve] = curren

 

                    new_file_name = file.replace(prve, curren)

                    print "\t重命名文件: %s -> %s" %(file, new_file_name)

                elif changePrefix == 1:

                    if file.startswith(old_prefix):

                        new_file_name = file.replace(old_prefix, new_prefix)

                elif addNewPrefix == 1:

                    new_file_name = new_prefix + new_file_name

                

                

                ignore_result = False

                for ignore_text in ignore_path_text:

                    if ignore_text in new_file_name:

                        ignore_result = True

 

                if ignore_result == False:

                    new_full_path = os.path.join(parent, new_file_name)

                    os.rename(old_full_path, new_full_path)

#这里是更改文件MD5值 一般更改只增加" " 空格,增加其他的字符肯能会导致项目无法正常运行

                    MDFfile = open(new_full_path,'a')

                    MDFfile.write(" ")

                    MDFfile.close

 

                    #在项目工程中改名

                    renameFileInXcodeProj(file, new_file_name)

 

                    #在可能引用的地方替换

                    old_file_base_name = os.path.splitext(file)[0]

                    NEWfileNames = new_file_name.split('.')

                    new_file_base_name = old_file_base_name.replace(fileNames[0], NEWfileNames[0])

                    if old_file_base_name == new_file_base_name:

                        print "这俩一样的呀    %s   ->>>  %s" %(old_file_base_name,new_file_base_name)

                    else:

                        print "真会改呀    %s   ->>>  %s" %(old_file_base_name,new_file_base_name)

                        renameInAllFile(old_file_base_name, new_file_base_name)

                    

            

                

                

                

 

    for parent, folders, files in os.walk(ios_src_path):

        for folder in folders:

            old_full_path = os.path.join(parent, folder)

            if folder.startswith(old_prefix) and not isNeedIgnore(old_full_path):

                foleders = new_file_name.split('/')

                str = ''

                chars = 'BbCcDdEeFfGgHhIiJjKkLlMmNnOoQqRrSsTtUuVvXxYyZz'

                length = len(chars) - 1

                random = Random()

                for i in range(9):

                    str += chars[random.randint(0, length)]

 

                new_folder_name = folder.replace(folders[len(folders) - 1], str)

                print "\t重命名文件夹: %s -> %s" %(folder, new_folder_name)

                new_full_path = os.path.join(parent, new_folder_name)

                os.rename(old_full_path, new_full_path)

                #在项目工程中改名

                renameFileInXcodeProj(folder, new_folder_name)

    print "finish\n"

 

def addPreFix():

    print "开始添加前缀"

    global add_prefix, ios_src_path

    for parent, folders, files in os.walk(ios_src_path):

        for file in files:

            old_full_path = os.path.join(parent, file)

            if not isNeedIgnore(old_full_path):

                new_file_name = add_prefix + file

                print "\t重命名文件: %s -> %s" %(file, new_file_name)

 

                new_full_path = os.path.join(parent, new_file_name)

                os.rename(old_full_path, new_full_path)

                #在项目工程中改名

                renameFileInXcodeProj(file, new_file_name)

 

                #在可能引用的地方替换

                old_file_base_name = os.path.splitext(file)[0]

                new_file_base_name = os.path.splitext(new_file_name)[0]

                renameInAllFile(old_file_base_name, new_file_base_name)

                renameInAllFile(add_prefix+add_prefix, add_prefix)

 

    for parent, folders, files in os.walk(ios_src_path):

        for folder in folders:

            old_full_path = os.path.join(parent, folder)

            if not isNeedIgnore(old_full_path):

                new_folder_name = add_prefix + folder

                print "\t重命名文件夹: %s -> %s" %(folder, new_folder_name)

                new_full_path = os.path.join(parent, new_folder_name)

                os.rename(old_full_path, new_full_path)

                #在项目工程中改名

                renameFileInXcodeProj(folder, new_folder_name)

    print "finish\n"

 

 

#----------------------------------------------------main------------------------------------------------        

def parse_args():

    global script_path, proj_ios_path

    parser = argparse.ArgumentParser(description='修改类名前缀工具.\n')

    parser.add_argument('--add_prefix', dest='add_prefix', type=str, required=False, default="", help='添加类名前缀')

    parser.add_argument('--old_prefix', dest='old_prefix', type=str, required=False, help='原类名前缀')

    parser.add_argument('--new_prefix', dest='new_prefix', type=str, required=False, help='替换后类名前缀')

    parser.add_argument('--ios_path', dest='ios_path', type=str, required=True, help='OC文件目录')

    parser.add_argument('--proj_path', dest='proj_path', type=str, required=False, default="", help='xx.xcodeproj路径')

    args = parser.parse_args()

    return args

 

def main():

    global old_prefix, new_prefix, ios_src_path, project_file_path, add_prefix

    app_args = parse_args()

 

    add_prefix = app_args.add_prefix

    old_prefix = app_args.old_prefix

    new_prefix = app_args.new_prefix

    ios_src_path = app_args.ios_path

    project_file_path = os.path.join(app_args.proj_path, "project.pbxproj")

    if not os.path.exists(ios_src_path):

        print "ios_path not exists: " +  ios_src_path

#        exit(0)

    if not os.path.exists(project_file_path):

        print "proj_path not exists: " +  project_file_path

 

    print "请提前备份文件夹或确认在版本管理软件中"

#    raw_input("回车继续执行")

#    if add_prefix and add_prefix != "":

#        addPreFix()

#        exit(0)

    dealWithIos()

 

 

if __name__ == "__main__":

    main()

  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用Python class实现批量修改文件名的完整代码: ```python import os class BatchRename: def __init__(self, path, prefix="", suffix="", replace_dict=None): self.path = path self.prefix = prefix self.suffix = suffix self.replace_dict = replace_dict def rename(self): for filename in os.listdir(self.path): # 获取文件的完整路径 file_path = os.path.join(self.path, filename) # 判断是否是文件,不是文件则跳过 if not os.path.isfile(file_path): continue # 替换文件名中的字符串 if self.replace_dict: for key, value in self.replace_dict.items(): filename = filename.replace(key, value) # 添加前缀和后缀 filename = self.prefix + filename + self.suffix # 获取文件的后缀名 ext = os.path.splitext(filename)[-1] # 生成新的文件名 new_filename = str(hash(filename)) + ext # 重命名文件 os.rename(file_path, os.path.join(self.path, new_filename)) if __name__ == '__main__': # 设置路径及参数 path = "/path/to/folder" prefix = "prefix_" suffix = "_suffix" replace_dict = {"old_str": "new_str"} # 实例化BatchRename类并调用rename方法 renamer = BatchRename(path, prefix, suffix, replace_dict) renamer.rename() ``` 以上代码中,BatchRename类的构造函数接收四个参数,分别是文件夹的路径(path)、文件名前缀(prefix)、文件名后缀(suffix)和需要替换的字符串字典(replace_dict)。rename方法遍历文件夹中的所有文件,根据参数进行文件名修改。其中,使用了hash函数生成新的文件名,以避免重名问题。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值