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读取Excel表格文件的方法

python读取Excel表格文件的方法

python读取Excel表格文件,例如获取这个文件的数据 python读取Excel表格文件,需要如下步骤: 1、安装Excel读取数据的库-----xlrd 直接pip insta...

djang常用查询SQL语句的使用代码

djang常用查询SQL语句的使用代码

将django语法和sql对应一下,希望对大家有所帮助 查询单个列的值 story.object.values_list("url", flat=True) SELECT `sto...

python基础教程之popen函数操作其它程序的输入和输出示例

python基础教程之popen函数操作其它程序的输入和输出示例

一、函数介绍 1.1 函数原型: 复制代码 代码如下:#include <stdio.h>FILE *popen(const char *command,const char...

pytorch自定义二值化网络层方式

任务要求: 自定义一个层主要是定义该层的实现函数,只需要重载Function的forward和backward函数即可,如下: import torch from torch.aut...

在Pycharm中将pyinstaller加入External Tools的方法

在Pycharm中将pyinstaller加入External Tools的方法

Pycharm: 2017.1.2 PyInstaller: 3.3.1 第一步:安装pyinstaller 网上有很多种方法,在此不赘述。pycharm中,安装很方便。 进入设置(co...