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. Leetcode ——Partition Equal Subset Sum

    Question Given a non-empty array containing only positive integers, find if the array can be partiti ...

  2. ThreadPoolExecutor执行过程分析

    ThreadPoolExecutor public ThreadPoolExecutor(int corePoolSize, int maximumPoolSize, long keepAliveTi ...

  3. Ubuntu上 配置Eclipse:安装CDT

    在最新的 Ubuntu Kylin 16.04 中安装了eclipse,在纠结了很久的网络问题之后,开始了eclipse的配置以便在上面运行ns3. 在官方网站上安装完 eclipse LUNA 之后 ...

  4. 解决复制到keil编辑器中汉字出现乱码情况

    https://blog.csdn.net/dxuehui/article/details/51123372 1.在菜单栏中选择'Edit'选项. 2.'Edit'选项中选择'Configuratio ...

  5. asp.net <asp:Repeater>下的 asp:LinkButton CommandArgument点击事件

    前台 <asp:Repeater ID="rptData" runat="server" OnItemCommand="rptData_Item ...

  6. #使用ListView更新数据出现闪烁解决办法

    //使用双缓冲:添加新类继承ListView 对其重写 public class DoubleBufferListView : ListView { public DoubleBufferListVi ...

  7. 编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串。 但是要保证汉字不被截半个,如“我ABC”4,应该截为“我AB”,输入“我ABC汉DEF”,6,应该输出为“我ABC”而不是“我ABC+汉的半个”。

    一.需要分析 1.输入为一个字符串和字节数,输出为按字节截取的字符串--->按照字节[byte]截取操作字符串,先将String转换成byte类型 2.汉字不可以截半--->汉字截半的话对 ...

  8. 《剑指offer》第二十三题(链表中环的入口结点)

    // 面试题23:链表中环的入口结点 // 题目:一个链表中包含环,如何找出环的入口结点?例如,在图3.8的链表中, // 环的入口结点是结点3. #include <iostream> ...

  9. SPP Net(Spatial Pyramid Pooling in Deep Convolutional Networks for Visual Recognition)论文理解

    论文地址:https://arxiv.org/pdf/1406.4729.pdf 论文翻译请移步:http://www.dengfanxin.cn/?p=403 一.背景: 传统的CNN要求输入图像尺 ...

  10. Redis之哈希类型命令

    Hash(哈希) Redis hash 是一个string类型的field和value的映射表,hash特别适合用于存储对象. Redis 中每个 hash 可以存储 232 - 1 键值对(40多亿 ...