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

软件发布|下载排行|最新软件

当前位置:首页IT学院IT技术

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

Thinking_boy1992   2021-03-28 我要评论
想了解python遍历一个目录,输出所有的文件名的实例的相关内容吗,Thinking_boy1992在本文为您仔细讲解python遍历一个目录,输出所有的文件名的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点: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遍历一个目录,输出所有的文件名的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。

猜您喜欢

Copyright 2022 版权所有 软件发布 访问手机版

声明:所有软件和文章来自软件开发商或者作者 如有异议 请与本站联系 联系我们