Python 实现文件操作管理系统

出发点

今天看了我文件夹中的壁纸,看到很多图片命名很乱,如果想往网站上导入图片,之后引用会很麻烦,因为名字不好记

所以想找到实现批量重命名的方法,毕竟有些类别的图片比较多,一个一个改也很麻烦

首先百度了一下,看到有如下方法

  • 直接ctrl+A全选来改,这样会出现如下的效果

image-20210920232328129.png

这个括号将来引用也很麻烦的

  • 和表格结合一起操作,对我这个excel弱爆还又不想学的菜鸟来说,瞟了两眼就不想看了
  • 下个批量修改文件名的软件,比如金舟批量重命名软件,但更多操作和功能需要开通会员

image-20210920232636622.png

  • 知道看见有人说直接python就好了,听起来挺不错

最后决定用python实现个基本跟上面哪个软件差不多的文件操作程序

思维导图

今天晚了,来不及实现所有功能了,索性就先把自己想到的功能设计出来

文件操作.png

基本代码设计

在网上搜索相关部分时,找到实现菜单式的代码,尝试了使用封装类来完成系统的设计,不过这样感觉代码有点冗长了

感觉用类是为了将来能够更快的读懂代码含义和添加新功能

import os
import re
import sys
class Menu:
    def __init__(self):
        self.rename = Rename()
        self.delete = Delete()
        self.choices = {
            "1": self.rename.run1,
            "2": self.delete.run2,
            "3": self.quit
        }

    def show_menu(self):
        print("*****欢迎来到Xherlock的文件管理系统*****")
        print("          (1) 重命名")
        print("          (2) 删除")
        print("          (3) 退出")

    def run(self):
        while True:
            self.show_menu()
            try:
                choice = input("请输入你的选择(数字)")
            except Exception as e:
                print("Please input a valid option!")
                continue
            choice = str(choice).strip()
            action = self.choices.get(choice)
            if action:
                action()
            else:
                print("没有{0}这个选项!请重新选择".format(choice))


    def quit(self):
        print("感谢您使用Xherlock的文件管理系统")
        sys.exit(0)


class Rename:
    def __init__(self):
        self.choices = {
            "1": self.rename_file,
            "2": self.rename_files,
            "3": self.quit1
        }

    def show_rename_menu(self):
        print("*****重命名*****")
        print("  (1) 单个")
        print("  (2) 批量")
        print("  (3) 退出")

    def run1(self):
        while True:
            self.show_rename_menu()
            try:
                choice1 = input("请输入你的选择(数字)")
            except Exception as e:
                print("Please input a valid option!")
                continue
            choice1 = str(choice1).strip()
            action = self.choices.get(choice1)
            if action:
                action()
            else:
                print("没有{0}这个选项!请重新选择".format(choice1))

    def rename_file(self):
        """
        重命名单个文件
        """
        pass


    def rename_files(self):
        """
        批量重命名文件
        """
        pass

    def quit1(self):
        sys.exit(0)

class Delete:
    def __init__(self):
        self.choices = {
            "1": self.del_file(),
            "2": self.del_folder(),
            "3": self.quit2
        }

    def show_del_menu(self):
        print("*****删除*****")
        print(" (1) 文件夹")
        print(" (2) 文件(单个)")
        print(" (3) 退出")

    def run2(self):
        while True:
            self.show_rename_menu()
            try:
                choice2 = input("请输入你的选择(数字)")
            except Exception as e:
                print("Please input a valid option!")
                continue
            choice2 = str(choice2).strip()
            action = self.choices.get(choice2)
            if action:
                action()
            else:
                print("没有{0}这个选项!请重新选择".format(choice2))

    def del_folder(self):
        """
        删除文件夹
        """
        pass


    def del_file(self):
        """
        删除文件
        """
        pass

    def quit2(self):
        sys.exit(0)


def isfile():
    """
    判断文件路径是否存在
    """
    pass


def isfolder():
    """
    判断文件夹是否存在
    """
    pass


if __name__ == '__main__':
    Menu().run()

之后抽时间来实现所有功能!明天中秋了!加油!

最后修改:2021 年 10 月 25 日
如果觉得我的文章对你有用,请随意赞赏