• python 获取当前文件夹下所有文件名


    os 模块下有两个函数:

    os.walk()

    os.listdir()

    1     # -*- coding: utf-8 -*-   
    2       
    3     import os  
    4       
    5     def file_name(file_dir):   
    6         for root, dirs, files in os.walk(file_dir):  
    7             print(root) #当前目录路径  
    8             print(dirs) #当前路径下所有子目录  
    9             print(files) #当前路径下所有非目录子文件  
     1     # -*- coding: utf-8 -*-   
     2       
     3     import os  
     4       
     5     def file_name(file_dir):   
     6         L=[]   
     7         for root, dirs, files in os.walk(file_dir):  
     8             for file in files:  
     9                 if os.path.splitext(file)[1] == '.jpeg':  
    10                     L.append(os.path.join(root, file))  
    11         return L  
    12 
    13 
    14 #其中os.path.splitext()函数将路径拆分为文件名+扩展名
     1     # -*- coding: utf-8 -*-  
     2     import os  
     3       
     4     def listdir(path, list_name):  #传入存储的list
     5         for file in os.listdir(path):  
     6             file_path = os.path.join(path, file)  
     7             if os.path.isdir(file_path):  
     8                 listdir(file_path, list_name)  
     9             else:  
    10                 list_name.append(file_path)  
  • 相关阅读:
    005. gitlab安装
    004. github使用
    003. git标签
    TS标红和报错解决(优化项)
    catalog连接数据库与sde权限问题
    博客新生企划
    HDU 7105 Power Sum
    HDU 7131 Nun Heh Heh Aaaaaaaaaaa
    BZOJ 1691 挑剔的美食家
    洛谷 4254 Blue Mary 开公司
  • 原文地址:https://www.cnblogs.com/strongYaYa/p/7200357.html
Copyright © 2020-2023  润新知