python遍历一个目录,输出所有的文件名的实例

yipeiwu_com5年前Python基础

python 获取一个文件夹内(包括子文件夹)所有文件的名字和路径

import os
dir = "e:\\"
for root, dirs, files in os.walk(dir):
  for file in files:
    print os.path.join(root,file)

或:

import os
path = r'e:\case'
fns = [os.path.join(root,fn) for root, dirs, files in os.walk(path) for fn in files]
for f in fns:
  print(f)
print(len(fns))
#coding=utf-8
import os

def GetFileList(dir, fileList):
  newDir = dir
  if os.path.isfile(dir):
    fileList.append(dir.decode('gbk'))
  elif os.path.isdir(dir): 
    for s in os.listdir(dir):
      #如果需要忽略某些文件夹,使用以下代码
      #if s == "xxx":
        #continue
      newDir=os.path.join(dir,s)
      GetFileList(newDir, fileList) 
  return fileList

list = GetFileList('D:\\workspace\\PyDemo\\fas', [])
for e in list:
  print e

以上这篇python遍历一个目录,输出所有的文件名的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

详解python 中in 的 用法

详解python 中in 的 用法

in在Python中是操作符,具体来说是成员操作符。就是对于序列(字符串,元组,列表)或集合(set)或映射(字典)这些数据类型做成员判断,自然成员判断的返回是在其中和不在其中,用Py...

Python实现CET查分的方法

Python CET自动查询方法需要用到的python方法模块有:sys、urllib2 本文实例讲述了Python实现CET查分的方法。分享给大家供大家参考。具体实现方法如下: 复制代...

Python基于list的append和pop方法实现堆栈与队列功能示例

Python基于list的append和pop方法实现堆栈与队列功能示例

本文实例讲述了Python基于list的append和pop方法实现堆栈与队列功能。分享给大家供大家参考,具体如下: #coding=utf8 ''''' 堆栈: 堆栈是一个后进先出...

Python的时间模块datetime详解

datetime模块用于是date和time模块的合集,datetime有两个常量,MAXYEAR和MINYEAR,分别是9999和1. datetime模块定义了5个类,分别是 1.d...

使用python opencv对目录下图片进行去重的方法

版本: 平台:ubuntu 14 / I5 / 4G内存 python版本:python2.7 opencv版本:2.13.4 依赖: 如果系统没有python,则需要进行安装 sudo...