@安徽网站建设,公司管理制度,西安注册公司网上申请入口,免费网站后台管理模板下载1. 在前位置中的所有文件夹内增加一个名为 abc 的新文件夹
import osdef create_abc_directories(root_dir.):# 获取当前目录下的所有目录subdirectories [d for d in os.listdir(root_dir) if os.path.isdir(os.path.join(root_dir, d))]# 在每个目录中创建名为abc的子目录f…1. 在前位置中的所有文件夹内增加一个名为 abc 的新文件夹
import osdef create_abc_directories(root_dir.):# 获取当前目录下的所有目录subdirectories [d for d in os.listdir(root_dir) if os.path.isdir(os.path.join(root_dir, d))]# 在每个目录中创建名为abc的子目录for directory in subdirectories:abc_dir os.path.join(root_dir, directory, abc)os.makedirs(abc_dir, exist_okTrue)print(f在 {directory} 目录中创建了 {abc_dir} 目录)if __name__ __main__:create_abc_directories()
用法保存为 *.py文件增加x权限执行命令 python *.py 2. 批量新建以文件 a.txt 内的名称命名的文件夹
import osdef create_folders_from_file(file_patha.txt):# 检查文件是否存在if not os.path.exists(file_path):print(f错误: 文件 {file_path} 不存在。)return# 打开文件并逐行读取文件名with open(file_path, r) as file:folder_names [line.strip() for line in file.readlines()]# 在当前位置创建文件夹如果同名文件夹已存在则跳过for folder_name in folder_names:folder_path os.path.join(os.getcwd(), folder_name)if os.path.exists(folder_path) and os.path.isdir(folder_path):print(f文件夹 {folder_name} 已存在跳过创建。)else:os.makedirs(folder_path, exist_okTrue)print(f已创建文件夹: {folder_path})if __name__ __main__:create_folders_from_file()
运行命令 python *.py ./a.txt
这个脚本首先检查文件a.txt是否存在然后逐行读取该文件中的名称。对于每个名称它构建文件夹路径并检查是否已经存在。如果存在则输出提示信息并跳过创建否则它将创建新的文件夹。 3.检查当前位置中的所有空文件夹并将其删除递归执行
import osdef remove_empty_directories(root_dir.):# 获取当前目录下的所有子目录和文件for root, dirs, files in os.walk(root_dir, topdownFalse):for directory in dirs:folder_path os.path.join(root, directory)# 检查目录是否为空if not os.listdir(folder_path):print(f删除空目录: {folder_path})os.rmdir(folder_path)if __name__ __main__:remove_empty_directories()
这个脚本使用os.walk函数遍历目录树从底层向上遍历目录。对于每个目录它检查是否为空如果是则删除该目录。
4.为当前位置下的子文件夹 在/home/link/中的同名子文件夹内 创建软连接软连接的名称是abcd
import os# 获取当前目录
current_dir os.getcwd()# 设置链接目录
link_dir /home/link# 遍历当前目录中的子目录
for dir_name in os.listdir(current_dir):# 检查是否是目录if os.path.isdir(os.path.join(current_dir, dir_name)):# 构建软链接的目标路径link_target os.path.join(link_dir, dir_name, abcd)# 创建软链接os.symlink(os.path.join(current_dir, dir_name), link_target)print(fCreated symlink for {dir_name} in {link_target}) 5.为当前位置中所有文件夹内的同名子文件夹批量创建软连接软连接名称以父文件夹来命名
编写一个python脚本其功能是 检查当前位置所有文件夹内是否有名称为abc的子文件夹如果有则在/home/link/中为子文件夹abc创建软链接软链接的名称为其父文件夹的名称
import os# 获取当前目录
current_dir os.getcwd()# 设置链接目录
link_dir /home/link# 遍历当前目录中的子目录
for dir_name in os.listdir(current_dir):dir_path os.path.join(current_dir, dir_name)abc_dir_path os.path.join(dir_path, abc)# 检查是否是目录且 abc 子目录存在if os.path.isdir(dir_path) and os.path.exists(abc_dir_path) and os.path.isdir(abc_dir_path):# 构建软链接的目标路径link_target os.path.join(link_dir, dir_name)# 创建软链接os.symlink(abc_dir_path, link_target)print(fCreated symlink for {abc_dir_path} in {link_target}) 例如/home/test/中有两个文件夹 公司A 公司B他们都有同样的文件夹abc则运行该脚本后会/home/link/中生成 公司A 公司B 两个软链接文件分别指向各自文件夹内的 abc
该用法适用的场景 在Company文件夹中有100个以公司名称命名的文件夹每个公司的文件夹中是以各种业务名称命名的子文件夹如果想对各个公司的同一个项目创建软连接到Projetc文件夹中则采用该脚本这样在 Projetc/项目X/ 文件家内就会出现以各个公司名为文件夹的软连接。
6. 查找具有相同名称关键字的文件夹
其功能是 比较 /home/A 和 /home/B 两个文件夹内的文件夹名称如果任意两个文件夹名称中有3个以上的中文字符相同则在a.txt文本中的同一行记录正两个文件夹的路径
import osdef get_chinese_characters(s):return [c for c in s if \u4e00 c \u9fff]def compare_folders(folder_a, folder_b, output_file):folders_a os.listdir(folder_a)folders_b os.listdir(folder_b)with open(output_file, w, encodingutf-8) as output:for folder_name_a in folders_a:for folder_name_b in folders_b:chinese_chars_a set(get_chinese_characters(folder_name_a))chinese_chars_b set(get_chinese_characters(folder_name_b))common_chars chinese_chars_a.intersection(chinese_chars_b)if len(common_chars) 3:path_a os.path.join(folder_a, folder_name_a)path_b os.path.join(folder_b, folder_name_b)output.write(f{path_a}\t{path_b}\n)if __name__ __main__:folder_a_path /home/Afolder_b_path /home/Boutput_file_path a.txtcompare_folders(folder_a_path, folder_b_path, output_file_path)print(Comparison completed. Results saved in a.txt.)