• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > python遍历目录的方法小结

python遍历目录的方法小结

作者: 字体:[增加 减小] 来源:互联网

通过本文主要向大家介绍了python 遍历目录,python遍历文件目录,python 创建目录,python 删除目录,python 进入目录等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

本文实例总结了python遍历目录的方法。分享给大家供大家参考,具体如下:

方法一使用递归:

"""
def WalkDir( dir, dir_callback = None, file_callback = None ):
  for item in os.listdir( dir ):
    print item;
    fullpath = dir + os.sep + item
    if os.path.isdir( fullpath ):
      WalkDir( fullpath, dir_callback, file_callback )
      if dir_callback: dir_callback( fullpath )
      else:
        if file_callback: file_callback( fullpath )"""
def DeleteDir( dir ):
  print "path"
    #os.rmdir( dir )
def DeleteFile( file ):
  try:
    print "file"
    #os.unlink( file )
  except WindowsError, e:
    pass
WalkDir( os.environ['TEMP'], DeleteDir, DeleteFile )

</div>

方法二:

import os, stat
def WalkDir( dir, dir_callback = None, file_callback = None ):
  for root, dirs, files in os.walk(dir):
    for f in files:
      print f
      file_path = os.path.join(root, f)
      if file_callback: file_callback( file_path )
      for d in dirs:
        dir_path = os.path.join(root, d)
        if dir_callback: dir_callback( dir_path )
def DeleteDir( dir ):
  print "path"
    #os.rmdir( dir )
def DeleteFile( file ):
  try:
    print "file"
    #os.unlink( file )
  except WindowsError, e:
    pass
WalkDir( os.environ['TEMP'], DeleteDir, DeleteFile )

</div>

更多关于Python相关内容感兴趣的读者可查看本站专题:《Python数据结构与算法教程》、《Python Socket编程技巧总结》、《Python函数使用技巧总结》、《Python字符串操作技巧汇总》、《Python入门与进阶经典教程》及《Python文件与目录操作技巧汇总》

希望本文所述对大家Python程序设计有所帮助。

</div>

您可能想查找下面的文章:

  • Python实现遍历目录的方法【测试可用】
  • Python遍历目录并批量更换文件名和目录名的方法
  • Python遍历目录中的所有文件的方法
  • Python遍历目录中的所有文件的方法
  • python遍历目录的方法小结
  • python遍历目录的方法小结
  • python实现一次创建多级目录的方法
  • python 生成目录树及显示文件大小的代码

相关文章

  • Python模块学习 filecmp 文件比较
  • Python常用正则表达式符号浅析
  • Python对文件和目录进行操作的方法(file对象/os/os.path/shutil 模块)
  • Python中使用PDB库调试程序
  • Python代理抓取并验证使用多线程实现
  • python脚本实现查找webshell的方法
  • Python下的twisted框架入门指引
  • python操作MySQL数据库具体方法
  • 使用Protocol Buffers的C语言拓展提速Python程序的示例
  • Python实现拷贝多个文件到同一目录的方法

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • 举例讲解Python的Tornado框架实现数据可视化的教程
    • Python抓取京东图书评论数据
    • 简单分析Python中用fork()函数生成的子进程
    • python中去空格函数的用法
    • 在Python中操作字符串之replace()方法的使用
    • Python的Flask开发框架简单上手笔记
    • 在tensorflow中设置保存checkpoint的最大数量实例
    • Python3基础之基本运算符概述
    • python多进程操作实例
    • python通过ssh-powershell监控windows的方法

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有