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'})