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

yipeiwu_com6年前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设计】。

相关文章

Django开发中复选框用法示例

本文实例讲述了Django开发中复选框用法。分享给大家供大家参考,具体如下: 一、查询数据库遍历所有的复选框 1、python查询数据库所有的tag # 新增文章 def add(r...

python使用Tkinter显示网络图片的方法

本文实例讲述了python使用Tkinter显示网络图片的方法。分享给大家供大家参考。具体实现方法如下: ''' tk_image_view_url_io.py display an...

Python实现感知机(PLA)算法

Python实现感知机(PLA)算法

我们主要讲解一下利用Python实现感知机算法。 算法一 首选,我们利用Python,按照上一节介绍的感知机算法基本思想,实现感知算法的原始形式和对偶形式。 #利用Python实现感...

Python之读取TXT文件的方法小结

方法一: <span style="font-size:14px;">#read txt method one f = open("./image/abc.txt")...

详解Swift中属性的声明与作用

一、引言 属性将值与类,结构体,枚举进行关联。Swift中的属性分为存储属性和计算属性两种,存储属性用于存储一个值,其只能用于类与结构体,计算属性用于计算一个值,其可以用于类,结构体和枚...