Python写的为文件添加前后缀小工具

Python版本

Python 3.7.3 [MSC v.1916 32 bit (Intel)] on win32

引入库

import os

import easygui as g

操作过程

采用GUI编程,提升了操作体验即使是初学者也能很快的上手
第一步:选择需要执行的操作
第一步:选择需要执行的操作
第二步:按照第一步的选择进行相关
第二步:按照第一步的选择进行相关
第三步:选择文件所在目录(此处选择的是目录)
第三步:选择文件所在目录
第四步:接下来选择需要更改的文件名第四步:接下来选择需要更改的文件名
第五步:单击‘OK’完成操作

完整代码

import os

import easygui as g
try:
    title_f = '自动添加前后缀小工具'

    choice = g.choicebox(msg='请选择文件处理方式', title=title_f, choices=['添加前缀', '添加后缀', '退出'])
    if choice == '退出':
        exit()

    str1 = g.enterbox(msg='请输入需要添加的%s' % choice[2:], title=title_f)
    file_dir = g.diropenbox(msg='请选择文件夹:', title=title_f)

    file_list = []
    os.chdir(file_dir)

    for each_file in os.listdir(file_dir):
        if os.path.isfile(os.getcwd() + os.sep + each_file):
            file_list.append(each_file)
        else:
            continue

    if len(file_list) == 0:
        g.msgbox('未选择任何文件')
        qianandhou()
    else:
        multchoice = g.multchoicebox(msg='请选择需要添加%s的文件(支持多选)' % choice[2:], title=title_f, choices=file_list,)
        t_multchoice = multchoice[:]
        if choice == '添加前缀':
            length = len(multchoice)
            for i in range(0, length):
                t_multchoice[i] = str1 + multchoice[i]
                os.rename(os.getcwd() + os.sep + multchoice[i], os.getcwd() + os.sep + t_multchoice[i])
            else:
                g.msgbox('前缀添加成功!')
        else:
            length = len(multchoice)
            for i in range(0, length):
                t_multchoice[i] = os.path.splitext(os.getcwd() + os.sep + multchoice[i])[0] + str1 + \
                                  os.path.splitext(os.getcwd() + os.sep + multchoice[i])[1]
                os.rename(os.getcwd() + os.sep + multchoice[i], t_multchoice[i])
            else:
                g.msgbox('后缀添加成功!')
except:
    g.msgbox('操作错误!\n即将退出')
    exit()
        
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值