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)

Python获取路径下所有文件名的更多相关文章

  1. python 获取当前目录下的文件目录和文件名

    python 获取当前目录下的文件目录和文件名   os模块下有两个函数: os.walk() os.listdir() 1 # -*- coding: utf-8 -*- 2 3 import os ...

  2. Python获取当前路径下的配置文件

    Python获取当前路径下的配置文件 有的时候想读取当前目录下的一个配置文件.其采用的办法是: import os # 获取当前路径 curr_dir = os.path.dirname(os.pat ...

  3. Java获取路径中的文件名(正则表达式)

    Java获取路径中的文件名(正则表达式) 目标 在这个路径中我想得到model2 /E:/2017-02-21--SoftWare/github/test/Java/poiDemo_word2exce ...

  4. python 获取路径

    获取目录路径和文件路径 import osfor root, dirs, files in os.walk(".", topdown=False): # ‘.’为获取脚本所在路径下 ...

  5. Delphi获取目录下所有文件名

    //获取一个文件夹下的所有文件 //不包括文件夹里面的文件 //ListBox1.Items:= searchfile('Z:\'); //注意,path后面要有'\'; function  Sear ...

  6. python 遍历, 获取目录下所有文件名和文件夹的方法-----os.walk(), os.listdir

    http://www.runoob.com/python/os-walk.html https://www.cnblogs.com/dreamer-fish/p/3820625.html 转载于:ht ...

  7. python 获取子目录下的所有文件的路径

    import os pathss=[] for root, dirs, files in os.walk(tarpath): path = [os.path.join(root, name) for ...

  8. python 获取当前目录下文件(转)

    今天继续整理原来写的 python 代码,下面是获取文件信息的 python 处理代码. 获取指定目录下文件的文件名以及文件的数量,然后列出其中还存在的目录名称: #!/usr/bin/env pyt ...

  9. python 获取路径不同方法的比较

    在软件中经常需要获取文件所在路径,方法有很多种( 例如 os.path.realpath(__file__), os.getcwd(), os.path.abspath(__file__),  sys ...

随机推荐

  1. Java中ArrayList和LinkedList区别 时间复杂度 与空间复杂度

    一般大家都知道ArrayList和LinkedList的大致区别:      1.ArrayList是实现了基于动态数组的数据结构,LinkedList基于链表的数据结构.      2.对于随机访问 ...

  2. Varnish 一般是放在 Nginx 前面还是后面的?

    1.varnish官网有写. 如果用ssl前面肯定得有nginx. 如果没有ssl看你实际需求.可以varnish,然后nginx,然后app. 看怎么设计了. 2.Varnish 通常是在两种情况下 ...

  3. OSError: [WinError 193] %1 不是有效的 Win32 应用程序。

    经过搜索查找,发现错误原因是我在win7 x64的机器上装了64位的python IDLE,不能有效load32位的dll,换成32位的python就好了.

  4. idea 2018注册码(激活码)永久性的

    2DZ8RPRSBU-eyJsaWNlbnNlSWQiOiIyRFo4UlBSU0JVIiwibGljZW5zZWVOYW1lIjoiY24gdHUiLCJhc3NpZ25lZU5hbWUiOiIiL ...

  5. Linux 设置定时任务 清空日志

    Step 1:前提是linux服务器安装了crond 定时任务需要crond服务的支持 1.启动方法 service crond restart 2.该服务默认是开机启动的 取消定时任务 1.全部取消 ...

  6. PHP概率,抽奖

    随机数,游标卡尺. 一个大饼,随机到哪个坑里,就中哪个奖. 大转盘! 公平公正! // 获取锦鲤 public function getGoldPig() { $pig_gift_bag = [ [ ...

  7. 百度编辑器 Ueditor 上传图片时打开文件夹的延迟问题,点击上传图片弹窗打开慢问题

      在使用 ueditor 开发时, 作为一个web文本编辑器使用时. 当点击上传图片时, 文件夹要延迟好久才能打开. 解决: 针对多图片上传, 将/ueditor/dialogs/image/ima ...

  8. spring boot 开发 org.springframework.context.ApplicationContextException: Unable to start web server;

    Error starting ApplicationContext. To display the conditions report re-run your application with 'de ...

  9. Flutter学习笔记(二)

    *.assets 当引用图片的时候,需要在pubspec.yaml的文件中的flutter下添加assets,类似于下面的样子: image.png 这里需要注意的是文件里的assets只要一个缩进即 ...

  10. URAL 1741 Communication Fiend

    URAL 1741 思路: dp 状态:dp[i][1]表示到第i个版本为正版的最少流量花费 dp[i][0]表示到第i个版本为盗版的最少流量花费 初始状态:dp[1][0]=dp[0][0]=0 目 ...