""" Auther:少校 Time:2025/4/18 16:08 越努力,越幸运 """ import os # os模块 - 提供了各种和文件相关的文件以及文件夹的相关函数 # 1. 创建文件夹 #a. # try: # os.mkdir('./新建文件夹/') # except: # print("文件已存在") #b. os.path.exists('./新建文件夹/') 如果存在返回true if not os.path.exists('./新建文件夹/'): os.mkdir('./新建文件夹/') # 3.获取指定路径中所有内容的名字 # os.listdir(文件路径) result = os.listdir('../week04') # 统计路径中文件和文件夹数量分别是多少 c1 =c2 =0 for x in result: if os.path.isdir(f'../week04/{x}'): c1 += 1 elif os.path.isfile(f'../week04/{x}'): c2 += 1 print(f"文件夹数量{c1},文件数量{c2}")
18.os模块
本节625字2025-04-18 17:11:09