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

from : http://www.cnblogs.com/strongYaYa/p/7200357.html

os 模块下有两个函数:

os.walk()

os.listdir()

 # -*- 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) #当前路径下所有非目录子文件  

 # -*- 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()函数将路径拆分为文件名+扩展名

   # -*- 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)  

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值