码迷,mamicode.com
首页 > 编程语言 > 详细

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

时间:2017-07-18 15:25:48      阅读:161      评论:0      收藏:0      [点我收藏+]

标签:utf-8   存储   class   coding   拆分   return   div   style   span   

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)  

 

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

标签:utf-8   存储   class   coding   拆分   return   div   style   span   

原文地址:http://www.cnblogs.com/strongYaYa/p/7200357.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!