python 中os.path模块用于操作文件或文件夹

os.path.exists(path) 判断文件路径是否存在

dir = "c:\windows"
if os.path.exists(dir) :
  print "dir exists"
else :
  print "no exists"

os.path.isfile(path) 判断path是否是文件

dir = "c:\windows\system32\cmd.exe"
if os.path.isfile(dir) :
  print "file exists"
else :
  print "no exists"

os.path.getsize(path) 获取path文件的大小

size = os.path.getsize(dir)
print size/1024

os.path.walk(path) 遍历path,返回一个三元组(dirpath, dirnames, filenames). dirpath表示遍历到的路径, dirnames表示该路径下的子目录名,是一个列表, filesnames表示该路径下的文件名,也是一个列表. 例如: 当遍历到c:\windows时,dirpath="c:\windows", dirnames是这个路径下所有子目录名的列表,filenames是这个路径下所有文件名的列表

for (root, dirs, files) in os.walk("C:\windows"):  列出windows目录下的所有文件和文件名
  for filename in files:
    print os.path.join(root,filename)

  for dirc in dirs:

    print os.path.join(root,dirc)

问题 1 获取给定文件夹的大小?

  要遍历文件的大小,只需要遍历文件内的所有文件,然后将所有文件夹的大小加起来

def getDirSzie(dir) :
for (root,dirs,files) in os.walk(dir,False) :
  Size = 0
  for filename in files :
    Size += os.path.getsize(os.path.join(root,filename))
  print root,Size/1024

问题 2 遍历一个文件夹的子目录,不遍历子目录的字目录?

os.listdir(path) 函数列出指定目录下的文件和文件夹

dir = 'c:/windows'
if os.path.exists(dir):
  dirs = os.listdir(dir)
  for dirc in dirs:
    print dirc
else :
  print "dir not exists"

问题3 删除指定目录下空的目录

for (root, dirs, files) in os.walk(path) :
  for item in dirs :
    dir = os.path.join(root, item)
    try :
      print dir
      os.rmdir(dir)
    except :
      pass

问题4  修改指定目录下所有文件的文件后缀

for (root,dirs,files) in os.walk(path) :
  for item in files :
    d = os.path.join(root, item)
    name = d + ".eml"
    os.rename(d, name)

python实现文件夹遍历的更多相关文章

  1. python学习笔记(六)文件夹遍历,异常处理

    python学习笔记(六) 文件夹遍历 1.递归遍历 import os allfile = [] def dirList(path): filelist = os.listdir(path) for ...

  2. python 关于文件夹的操作

    在python中,文件夹的操作主要是利用os模块来实现的, 其中关于文件夹的方法为:os.lister() , os.path.join() , os.path.isdir() #  path 表示文 ...

  3. Python打包文件夹的方法小结(zip,tar,tar.gz等)

    本文实例讲述了Python打包文件夹的方法.分享给大家供大家参考,具体如下: 一.zip ? 1 2 3 4 5 6 7 8 9 10 11 import os, zipfile #打包目录为zip文 ...

  4. python操作txt文件中数据教程[3]-python读取文件夹中所有txt文件并将数据转为csv文件

    python操作txt文件中数据教程[3]-python读取文件夹中所有txt文件并将数据转为csv文件 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考文献 python操作txt文件中 ...

  5. HALCON初步:文件夹遍历,文件筛选,文件名拆分,图片读取及保存

    [1]文件夹遍历 list_image_files ( : : ImageDirectory, Extensions, Options : ImageFiles) ImageDirectory: 文件 ...

  6. 用Python打开文件夹

    用Python读取文件夹, 然后打开文件 下面读取到文件的每一个内容, 然后加上路径 import os path = r'../Downloads/text/content' for filenam ...

  7. 如何使用python 新建文件夹以及递归创建文件夹

    转载:如何使用python 新建文件夹以及递归创建文件夹 | 酷python (coolpython.net) 1. os.mkdir 使用python创建文件夹,通常使用os.mkdir方法,在使用 ...

  8. python文件夹遍历,文件操作,获取文件修改创建时间

    在Python中,文件操作主要来自os模块,主要方法如下: os.listdir(dirname):列出dirname下的目录和文件os.getcwd():获得当前工作目录os.curdir:返回当前 ...

  9. python:创建文件夹:写入文本1:读取txt:读取Excel文件遍历文件夹:

    https://blog.csdn.net/u011956147/article/details/80369731 创建文件夹: import osimport shutil def buildfil ...

随机推荐

  1. 使用鼠标监听器,使鼠标悬停在JTable某行时背景色改变

    一.需要知道的事实: 1.当鼠标悬停在JTable上时,相应的格子(cell)的渲染器(TableCellRenderer)的渲染方法(getTableCellRenererComponent)会被调 ...

  2. CWorkBooks、CWorkBook、CWorkSheets、CWorkSheet、CRange

    我们使用VC++操作Excel,对于Excel编程来说肯定少不了要遇到六个最基本的类: CApplication.CWorkBook.CWorkBooks.CWorkSheet.CWorkSheets ...

  3. oracle之trunc(sysdate)

    --截取后得到的仍为date数据类型 select trunc(sysdate) from dual;--2017-03-13 00:00:00select trunc(sysdate+1) from ...

  4. JavaScript------字符串与HTML格式相互转换

    转载: http://blog.sina.com.cn/s/blog_4cb0b0fc0100aoo1.html 代码:: 1.将字符转换成Html function encodeHtml(str){ ...

  5. Java课后思考题

    1.简述path和classpath的区别. path:path环境变量是系统环境变量中的一种,它用于保存一系列可执行文件的路径,每个路径之间以分号分隔.当在命令行窗口运行一个可执行文件时,操作系统首 ...

  6. MySQL性能优化(一)-- 存储引擎和三范式

    一.MySQL存储引擎 存储引擎说白了就是如何存储数据.如何为存储的数据建立索引和如何更新.查询数据等技术的实现方法.因为在关系数据库中数据的存储是以表的形式存储的,所以存储引擎也可以称为表类型(即存 ...

  7. Java编程基本概念

    1.标识符 ①用于给变量.类和方法命名(类名首字母大写,变量和方法名首字母小写并遵循驼峰原则)②标识符的命名规范: ■标识符必须以字母.下划线和美元符$开头. ■标识符其他部分可以是字母.下划线.美元 ...

  8. Change Base

    Given an integer m in base B (2 ≤ B ≤ 10) (m contains no more than 1000 digits), find the value of t ...

  9. ThinkPHP流程控制!

    IF判断: 在thinkphp 中不能使用 <> 这样的尖括号: <if condition='表达式'> <elseif condition='表达式'/> &l ...

  10. nginx配置文件内容详情及基本属性配置

    Nginx的配置文件nginx.conf配置详解如下: user nginx nginx ; Nginx用户及组:用户 组.window下不指定 worker_processes 8; 工作进程:数目 ...