zoukankan      html  css  js  c++  java
  • 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路径名
    os.listdir(root_path)  #列出root_path文件夹下所有的目录与文件
    def analyze_path(path):
        print("abspath:", os.path.abspath(path))
        print("basename:", os.path.basename(path))
        print("dirname:", os.path.dirname(path))
        print("exists:", os.path.exists(path))
        print("atime:", os.path.getatime(path))
        print("normcase:", os.path.normcase(path))
        print("normpath:", os.path.normpath(path))
        print("realpath:", os.path.realpath(path))
        print("join:", os.path.join("F:\test\", os.path.basename(path)))
        print("splitdrive:", os.path.splitdrive(path))
        print("splitunc:", os.path.splitunc(path))
    
    def main():
        path = "E:\Users\Administrator\eclipse-workspace\com.leagsoft\test\example.csv"
        analyze_path(path)
        
    
    if __name__ == "__main__":
        main()

    输出:

    abspath: E:UsersAdministratoreclipse-workspacecom.leagsoft	estexample.csv
    basename: example.csv
    dirname: E:UsersAdministratoreclipse-workspacecom.leagsoft	est
    exists: True
    atime: 1537200000.0
    normcase: e:usersadministratoreclipse-workspacecom.leagsoft	estexample.csv
    normpath: E:UsersAdministratoreclipse-workspacecom.leagsoft	estexample.csv
    realpath: E:UsersAdministratoreclipse-workspacecom.leagsoft	estexample.csv
    join: F:	estexample.csv
    splitdrive: ('E:', '\Users\Administrator\eclipse-workspace\com.leagsoft\test\example.csv')
    splitunc: ('', 'E:\Users\Administrator\eclipse-workspace\com.leagsoft\test\example.csv')

     遍历文件和目录:

     rootdir = 'F:data'
     list = os.listdir(rootdir) #列出文件夹下所有的目录与文件
     for i in range(0,len(list)):
            path = os.path.join(rootdir,list[i])
            if os.path.isfile(path):
                   #你想对文件的操作

    Reference:

    [1] http://www.cnblogs.com/WonderHow/p/4403727.html

  • 相关阅读:
    使用Windbg调试.Net应用程序 [转]
    jquery的$.extend和$.fn.extend作用及区别
    java获取ip
    在IDEA中使用MyBatis Generator逆向工程生成代码
    github结合TortoiseGit使用sshkey,无需输入账号和密码
    Mapped Statements collection does not contain value for com.xxxx.dao.impl.AreaDAOImpl.findByCode
    idea设置tomcat虚拟路径的两种方法
    spring MVC中传递的参数对象中包含list的情况
    PowerDsigner 16逆向工程导入mysql
    [C++]Qt文本操作(按行读写)
  • 原文地址:https://www.cnblogs.com/hoojjack/p/9671048.html
Copyright © 2011-2022 走看看