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

相关文章

如何通过python画loss曲线的方法

如何通过python画loss曲线的方法

1. 首先导入一些python画图的包,读取txt文件,假设我现在有两个模型训练结果的records.txt文件 import numpy as np import matplotl...

python画图把时间作为横坐标的方法

python画图把时间作为横坐标的方法

1、需要将时间字符串转换成datetime类型,语法:data[‘time'] = pd.to_datetime(data[‘time']) 2、将时间列设置成索引列data.set_i...

python 安装virtualenv和virtualenvwrapper的方法

1. 首先介绍pip常用命令 pip安装命令: pip install package_name pip升级命令:pip install –ungrage package_name...

将Emacs打造成强大的Python代码编辑工具

将Emacs打造成强大的Python代码编辑工具

基本配置 Emacs本身提供了python-mode,输入M-x python-mode,就可以进入python模式。相应地,会在菜单栏出现Python菜单。当然,一般来讲,如果是.py...

基于Python 装饰器装饰类中的方法实例

基于Python 装饰器装饰类中的方法实例

title: Python 装饰器装饰类中的方法 comments: true date: 2017-04-17 20:44:31 tags: ['Python', 'Decorate'...