zoukankan      html  css  js  c++  java
  • python文件操作总结

    1.python实现文件夹遍历

    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:windowssystem32cmd.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是这个路径下所有子目录名的列表,dirnames是这个路径下所有文件名的列表

    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)

    2.python遍历文件夹下的文件

    在读文件的时候往往需要遍历文件夹,python的os.path包含了很多文件、文件夹操作的方法。下面列出:

    os.path.abspath(path) #返回绝对路径
    os.path.basename(path) #返回文件名
    os.path.commonprefix(list) #返回多个路径中,所有path共有的最长的路径。
    os.path.dirname(path) #返回文件路径
    os.path.exists(path)  #路径存在则返回True,路径损坏返回False
    os.path.lexists  #路径存在则返回True,路径损坏也返回True
    os.path.expanduser(path)  #把path中包含的"~"和"~user"转换成用户目录
    os.path.expandvars(path)  #根据环境变量的值替换path中包含的”$name”和”${name}”
    os.path.getatime(path)  #返回最后一次进入此path的时间。
    os.path.getmtime(path)  #返回在此path下最后一次修改的时间。
    os.path.getctime(path)  #返回path的大小
    os.path.getsize(path)  #返回文件大小,如果文件不存在就返回错误
    os.path.isabs(path)  #判断是否为绝对路径
    os.path.isfile(path)  #判断路径是否为文件
    os.path.isdir(path)  #判断路径是否为目录
    os.path.islink(path)  #判断路径是否为链接
    os.path.ismount(path)  #判断路径是否为挂载点()
    os.path.join(path1[, path2[, ...]])  #把目录和文件名合成一个路径
    os.path.normcase(path)  #转换path的大小写和斜杠
    os.path.normpath(path)  #规范path字符串形式
    os.path.realpath(path)  #返回path的真实路径
    os.path.relpath(path[, start])  #从start开始计算相对路径
    os.path.samefile(path1, path2)  #判断目录或文件是否相同
    os.path.sameopenfile(fp1, fp2)  #判断fp1和fp2是否指向同一文件
    os.path.samestat(stat1, stat2)  #判断stat tuple stat1和stat2是否指向同一个文件
    os.path.split(path)  #把路径分割成dirname和basename,返回一个元组
    os.path.splitdrive(path)   #一般用在windows下,返回驱动器名和路径组成的元组
    os.path.splitext(path)  #分割路径,返回路径名和文件扩展名的元组
    os.path.splitunc(path)  #把路径分割为加载点与文件
    os.path.walk(path, visit, arg)  #遍历path,进入每个目录都调用visit函数,visit函数必须有3个参数(arg, dirname, names),dirname表示当前目录的目录名,names代表当前目录下的所有文件名,args则为walk的第三个参数
    os.path.supports_unicode_filenames  #设置是否支持unicode路径名
    下面给出一种遍历的实现:
    1 rootdir = 'F:data'
    2 list = os.listdir(rootdir) #列出文件夹下所有的目录与文件
    3 for i in range(0,len(list)):
    4        path = os.path.join(rootdir,list[i])
    5        if os.path.isfile(path):
    6               #你想对文件的操作
  • 相关阅读:
    trailRenderer
    通过sysobjects快速查找SQLServer中是否有某个表、视图、存储过程等对象实操
    浅谈信息系统(IT)项目管理-序幕
    使用open xml 判断sharepoint文档是否损坏
    Sharepoint the file is locked for use domainuser edit.文件被锁定,解锁方式
    sharepoint 列表库指定序号规则
    Biztalk 宏
    Biztalk 在流程中定义将消息保存为文件的文件名
    Biztalk 2013 新特性简介(英)
    devexpress gridview,selectedrowchanged
  • 原文地址:https://www.cnblogs.com/yanzi-meng/p/8618021.html
Copyright © 2011-2022 走看看