生成一个类,里面能批量创建文件夹和文件,也能批量删除文件夹和文件,也能批量对文件夹和文件重命名

import os

 

class FileManager:

    def __init__(self, base_path):

        self.base_path = base_path

 

    def create_folders(self, folder_names):

        """

        批量创建文件夹

        :param folder_names: 文件夹名称列表

        """

        for folder_name in folder_names:

            folder_path = os.path.join(self.base_path, folder_name)

            if not os.path.exists(folder_path):

                os.makedirs(folder_path)

                print(f"Created folder: {folder_path}")

            else:

                print(f"Folder already exists: {folder_path}")

 

    def create_files(self, file_paths_and_contents):

        """

        批量创建文件并写入内容

        :param file_paths_and_contents: 包含文件路径和内容的字典,例如 {'path/to/file1.txt': 'content1', ...}

        """

        for file_path, content in file_paths_and_contents.items():

            full_path = os.path.join(self.base_path, file_path)

            dir_path = os.path.dirname(full_path)

            if not os.path.exists(dir_path):

                os.makedirs(dir_path)

            with open(full_path, 'w') as file:

                file.write(content)

                print(f"Created file with content: {full_path}")

 

    def delete_folders(self, folder_names):

        """

        批量删除文件夹(递归删除)

        :param folder_names: 文件夹名称列表

        """

        for folder_name in folder_names:

            folder_path = os.path.join(self.base_path, folder_name)

            if os.path.exists(folder_path):

                try:

                    os.rmdir(folder_path) # 尝试删除空文件夹

                except OSError:

                    shutil.rmtree(folder_path) # 如果文件夹不为空,则递归删除

                print(f"Deleted folder: {folder_path}")

            else:

                print(f"Folder does not exist: {folder_path}")

 

    def delete_files(self, file_names):

        """

        批量删除文件

        :param file_names: 文件名列表(不包含路径)

        """

        for file_name in file_names:

            file_path = os.path.join(self.base_path, file_name)

            if os.path.exists(file_path):

                os.remove(file_path)

                print(f"Deleted file: {file_path}")

            else:

                print(f"File does not exist: {file_path}")

 

    def rename_folders(self, old_to_new_names):

        """

        批量重命名文件夹

        :param old_to_new_names: 包含旧名称和新名称的字典,例如 {'old_folder': 'new_folder', ...}

        """

        for old_name, new_name in old_to_new_names.items():

            old_path = os.path.join(self.base_path, old_name)

            new_path = os.path.join(self.base_path, new_name)

            if os.path.exists(old_path):

                os.rename(old_path, new_path)

                print(f"Renamed folder: {old_path} to {new_path}")

            else:

                print(f"Folder does not exist: {old_path}")

 

    def rename_files(self, old_to_new_names):

        """

        批量重命名文件

        :param old_to_new_names: 包含旧文件名和新文件名的字典,例如 {'old_file.txt': 'new_file.txt', ...}

        """

        for old_name, new_name in old_to_new_names.items():

            old_path = os.path.join(self.base_path, old_name)

            new_path = os.path.join(self.base_path, new_name)

            if os.path.exists(old_path):

                os.rename(old_path, new_path)

                print(f"Renamed file: {old_path} to {new_path}")

            else:

                print(f"File does not exist: {old_path}")

 

# 注意:为了使用shutil.rmtree,需要在文件的开始部分导入shutil模块

# import shutil

 

# 示例用法

file_manager = FileManager('/path/to/your/directory')

 

# 批量创建文件夹

file_manager.create_folders(['folder1', 'folder2/subfolder1'])

 

# 批量创建文件

file_manager.create_files({

    'file1.txt': 'This is content for file1.txt',

    'folder2/subfolder1/file2.txt': 'This is content for file2.txt'

})

 

# 批量删除文件夹(需要导入shutil)

# file_manager.delete_folders(['folder1', 'folder2'])

 

# 批量删除文件

file_manager.delete_files(['file1.txt', 'folder2/subfolder1/file2.txt'])

 

# 批量重命名文件夹

file_manager.rename_folders({'folder1': 'new_folder1', 'folder2': 'new_folder2'})

 

# 批量重命名文件

file_manager.rename_files({'file1.txt': 'renamed_file1.txt', 'folder2/subfolder1/file2.txt': 'renamed_file2.txt'})

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值