目录
一、遍历一个文件夹,打印出该文件夹下所有的py文件
# 获取当前工作目录 print(os.getcwd()) # 列出指定目录下的所有文件和子目录 print(os.listdir(os.getcwd())) # l = os.listdir(os.getcwd()) # 当前文件夹下的py文件 py_list = [i for i in os.listdir(os.getcwd()) if len(i.split('.'))>1 and i.split('.')[1]=='py'] print(py_list) # 指定文件下的py文件 - 绝对路径 py_list_0 = [i for i in os.listdir('D:\\python\\Test\\mydjango\\app_0' ) if len(i.split('.')) > 1 and i.split('.')[1] == 'py'] print(py_list_0) # 指定文件夹下的py文件 - 相对路径 py_list_1 = [i for i in os.listdir('./app_0') if len(i.split('.')) > 1 and i.split('.')[1] == 'py'] print(py_list_1)