今天看啥  ›  专栏  ›  志军

9个工作必备的Python自动化代码

志军  · 公众号  · Python  · 2024-03-14 16:41
1.自动化文件管理1.1 对目录中的文件进行排序```# Python script to sort files in a directory by their extensionimport osfromshutil import movedef sort_files(directory_path):for filename in os.listdir(directory_path): if os.path.isfile(os.path.join(directory_path, filename)): file_extension = filename.split('.')[-1] destination_directory = os.path.join(directory_path, file_extension) if not os.path.exists(destination_directory): os.makedirs(destination_directory) move(os.path.join(directory_path, filename), os.path.join(destination_directory, filename))```说明:此Python脚本根据文件扩展名将文件分类到子目录中,以组织目录中的文件。它识别文件扩展名并将文件移动到适当的子目录。这对于整理下载文件夹或组织特定项目的文件很有用。1.2 删除空文件夹```# Python script to remove empty folders in a directoryimport osdef remove_empty_folders(directory_path):for root, dirs, f ………………………………

原文地址:访问原文地址
快照地址: 访问文章快照