Python实现文件名称批量添加后缀工具

做了一个文件批量添加后缀名的小工具,为了方便操作,使用WPF制作了一个窗口用来输入参数

 C#代码如下:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Security.Cryptography;
using System.Windows.Forms;
using System.Diagnostics;
using System.Collections;

namespace RenameFiles
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window
    {
        static string m_suffixStr = "";
        static string m_dstPath = "";
        public MainWindow()
        {
            InitializeComponent();
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            if (m_dstPath.Length==0)
            {
                System.Windows.Forms.MessageBox.Show("重命名路径不能为空");
                return;
            }

            string curPath = System.IO.Directory.GetCurrentDirectory();
            System.Diagnostics.Process.Start("python", curPath+"/bridge.py "+ m_dstPath+" "+ m_suffixStr);
        }

        private bool HasDirectory(string[] files)
        {
            if (files == null)
                return false;
            for (int i = 0; i < files.Length; i++)
            {
                if (System.IO.Directory.Exists(files[i]))
                {
                    return true;
                }
            }
            return false;
        }

        /// <summary>
        /// 放下文件获取路径
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void onDropFile(object sender, System.Windows.DragEventArgs e)
        {
            var files = (string[])e.Data.GetData(System.Windows.DataFormats.FileDrop);
            if (files == null)
            {
                return;
            }
            for (int i = 0; i < files.Length; i++)
            {
                label_floder.Content = files[i];
                m_dstPath= files[i];
            }
        }

        private void textBox_TextChanged(object sender, TextChangedEventArgs e)
		{
            m_suffixStr = textBox.Text;
        }
	}
}

Python实现如下:

#!/usr/bin/python
# -*- coding:UTF-8 -*-

import os,os.path
import sys
import shutil
from optparse import OptionParser

#######functions##########
def makeDir(path):
	if os.path.exists(path):
		shutil.rmtree(path)
	os.mkdir(path)

# 循环递归遍历文件夹
def getFloderFiles(file_dir):
	arr=[]
	fs = os.listdir(file_dir)
	for dir in fs:
		tmp_path = os.path.join(file_dir, dir)
		if os.path.isfile(tmp_path):
			arr.append(tmp_path)
		else:
			# 是文件夹,则递归调用
			arr.extend(getFloderFiles(tmp_path))
	return arr

def init(srcPath,suffix):
	#获取当前文件夹的上一级目录
	curPath=os.path.abspath(os.path.dirname(os.path.dirname(os.path.realpath(__file__))))
	print(curPath)

	dstPath=curPath+'\\newfiles'
	makeDir(dstPath)

	allFilesArr=getFloderFiles(srcPath)
	for key in allFilesArr:
		#分离目录名和文件名
		tu0=os.path.split(key)
		#分离文件名和扩展名
		tu1=os.path.splitext(tu0[1])
		#重命名文件
		newFileName=dstPath+'\\'+tu1[0]+suffix+tu1[1]
		shutil.copyfile(key,newFileName)

	print('**********sucess**********')
	print('**********sucess**********')
	print('**********sucess**********')

# -------------- main --------------
if __name__ == '__main__':

	parser = OptionParser()
	parser.add_option("-p", "--path", dest="path", help='rename floder')
	parser.add_option("-s", "--suffix", dest="suffix", help='rename file add suffix')
	(opts, args) = parser.parse_args()

	init(opts.path,opts.suffix)
		

工具地址:文件名批量添加后缀工具

 提取码:hmoa  

猜你喜欢

转载自blog.csdn.net/auccy/article/details/121406392