batch change file name

本文提供了一个Python脚本,用于更改指定目录中的文件名。脚本通过输入目录路径,遍历目录内容,提取文件名前缀,并尝试进行重命名操作。在重命名过程中,脚本会捕获并提示可能发生的错误,如文件已存在或权限不足等问题。
部署运行你感兴趣的模型镜像
#-------------------------------------------------------------------------------
# Name:        changeDirName
# Purpose:     change directory name
#
# Author:      wind
#
# Created:     29-09-2011
# Copyright:   (c) wind 2011
# Licence:     <your licence>
#-------------------------------------------------------------------------------
#!/usr/bin/env python


import wx
import os


def changeDirName():
    dir = input("please input directory path(example: r'e:\dir'):  ")
    for i in os.listdir(dir):
        newName = oldName = i
        newName = oldName.split('-')[0]
        try:
            os.rename(dir+r'/'+oldName, dir+r'/'+newName)
        except ValueError:
            wx.MessageBox(dir+r'/'+newName, 'Error when rename the file!', wx.OK|wx.ICON_WARNING)
        except NameError:
            wx.MessageBox(dir+r'/'+newName, 'Error when rename the file!', wx.OK|wx.ICON_WARNING)
        except OSError:
            wx.MessageBox(dir+r'/'+newName, 'The file is already exist!', wx.OK|wx.ICON_WARNING)
    wx.MessageBox(dir, 'Change file name success!', wx.OK|wx.ICON_WARNING)


if __name__ == '__main__':
    app = wx.App(False)
    changeDirName()
    app.MainLoop()

您可能感兴趣的与本文相关的镜像

Python3.10

Python3.10

Conda
Python

Python 是一种高级、解释型、通用的编程语言,以其简洁易读的语法而闻名,适用于广泛的应用,包括Web开发、数据分析、人工智能和自动化脚本

修改 ONNX 模型的 batch 可以通过以下几种方法实现: ### 通过 onnx 库修改 ONNX 模型的 batch 为动态 可以使用以下代码将 ONNX 模型的输入 batch 修改为动态: ```python import onnx def change_input_dim(model): # 使用一个未被其他维度使用的符号名称 sym_batch_dim = "N" # 或使用一个实际的值 # actual_batch_dim = "4" # 以下代码将每个输入的第一个维度修改为 batch 维度 # 根据需要进行修改,注意这要求所有输入具有相同的 batch 维度 inputs = model.graph.input for input in inputs: # 省略了检查。这里假设所有输入都是张量,并且第一个维度存在 # 根据需要添加检查 dim1 = input.type.tensor_type.shape.dim[0] # 将维度更新为符号值 dim1.dim_param = sym_batch_dim # 或更新为实际值 # dim1.dim_value = actual_batch_dim def apply(transform, infile, outfile): model = onnx.load(infile) transform(model) onnx.save(model, outfile) # 请替换为实际的文件路径 onnx_pth = 'your_input_model.onnx' save_pth = 'your_output_model.onnx' apply(change_input_dim, onnx_pth, save_pth) ``` 上述代码通过 `change_input_dim` 函数将 ONNX 模型中所有输入的第一个维度修改为符号值 `N`,从而实现动态 batch 的设置 [^2]。 ### 将 ONNX 模型的 batch 固定为某个值 以下代码可以将 ONNX 模型的 batch 固定为指定的大小: ```python import onnx def change_input_dim(model): sym_batch_dim = "batch" actual_batch_dim = 4 graph = model.graph nodes = list(graph.input) + list(graph.value_info) + list(graph.output) for node in nodes: if not len(node.type.tensor_type.shape.dim): continue if node.type.tensor_type.shape.dim[0].dim_param == sym_batch_dim: print(node) node.type.tensor_type.shape.dim[0].dim_value = actual_batch_dim def apply(transform, infile, outfile): model = onnx.load(infile) transform(model) onnx.save(model, outfile) # 请替换为实际的文件路径 apply(change_input_dim, 'input-file-name', 'output-file-name') ``` 这段代码将模型中所有第一个维度参数为 `batch` 的节点的 batch 大小固定为 4 [^3]。 ### 实际应用示例 以下是一个实际应用上述 `apply` 函数修改 batch 的示例: ```python apply(change_input_dim, './model/VehicleTypeClassify_0_0_0_1.onnx', './model/VehicleTypeClassify_0_0_0_1_16.onnx') ``` 此示例将 `./model/VehicleTypeClassify_0_0_0_1.onnx` 模型的 batch 进行修改后保存为 `./model/VehicleTypeClassify_0_0_0_1_16.onnx` [^4]。 需要注意的是,导出的 ONNX 模型分为静态和动态输入两种,一般用户在导出后进行 onnxsim 操作,可能会导致某些非全卷积的模型修改 batch 失败,比如 Transformer 类模型中 reshape 的 attr 属性会固定,这种情况下从源头重新导出是最佳选择。在没有源头的情况下,可以尝试上述方法尽量完成 batch 的修改,并固化每个 op 的输入输出 tensor 为正确 shape,操作依赖于 `onnx`、`onnxsim` 和 `onnx_graphsurgeon` [^1]。
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值