zoukankan      html  css  js  c++  java
  • Arcgis-Python_02-Python路径处理

    前言

    要操作文件,离不开路径处理,其中使用最多的是os文件路径模块,在文件开头导入

    import os

    路径中转义字符处理(建议使用第三种)

    \	"C:\Users\user\Desktop\test\a.txt"
    /	"C:/Users/user/Desktop/test/a.txt"
    r	r"C:UsersuserDesktop	esta.txt"
    

    脚本文件点此下载。

    os.path模块常用方法

    简介

    方法 说明
    os.path.abspath(path) 返回绝对路径
    os.path.basename(path) 返回文件名
    os.path.dirname(path) 返回文件路径
    os.path.split(path) 把路径分割成 dirname 和 basename,返回一个元组
    os.path.splitdrive(path) 一般用在 windows 下,返回驱动器名和路径组成的元组
    os.path.splitext(path) 分割路径,返回路径名和文件扩展名的元组
    os.path.exists(path) 判断路径是否存在
    os.path.isfile(path) 判断是否为文件
    os.path.islink(path) 判断路径是否为链接
    os.path.isdir(path) 判断是否为路径
    os.path.join(path,filename) 将目录和文件名合成为一个路径
    os.path.expanduser(path) 把path中包含的""和"user"转换成用户目录
    os.path.getatime(path) 返回最近访问时间(浮点型秒数)
    os.path.getmtime(path) 返回最近文件修改时间
    os.path.getctime(path) 返回文件 path 创建时间
    os.path.getsize(path) 返回文件大小,如果文件不存在就返回错误

    实例

    import os
    print os.getcwd()
    path = r"C:UsersAdminDesktop	esta.txt"
    print "os.path.abspath(path)"
    print os.path.abspath(path)
    print "os.path.basename(path)"
    print os.path.basename(path)
    print "os.path.dirname(path)"
    print os.path.dirname(path)
    print "os.path.split(path)"
    print os.path.split(path)
    print "os.path.splitdrive(path)"
    print os.path.splitdrive(path)
    print "os.path.splitext(path)"
    print os.path.splitext(path)
    print "os.path.exists(path)"
    print os.path.exists(path)
    print "os.path.isfile(path)"
    print os.path.isfile(path)
    print "os.path.isdir(path)"
    print os.path.isdir(path)
    print "os.path.join(path,filename)"
    print os.path.join(os.path.dirname(path),os.path.basename(path))
    

    运行结果

    C:UsersAdminDesktop
    os.path.abspath(path)
    C:UsersAdminDesktop	esta.txt
    os.path.basename(path)
    a.txt
    os.path.dirname(path)
    C:UsersAdminDesktop	est
    os.path.split(path)
    ('C:\Users\Admin\Desktop\test', 'a.txt')
    os.path.splitdrive(path)
    ('C:', '\Users\Admin\Desktop\test\a.txt')
    os.path.splitext(path)
    ('C:\Users\Admin\Desktop\test\a', '.txt')
    os.path.exists(path)
    True
    os.path.isfile(path)
    True
    os.path.isdir(path)
    False
    os.path.join(path,filename)
    C:UsersAdminDesktop	esta.txt
    

    os.getcwd()

    概述

    os.getcwd()方法用于获取当前执行脚本的路径

    语法

    os.getcwd()
    

    参数

    返回值

    字符串路径

    实例

    代码

    import os
    print os.getcwd()
    

    运行结果

    C:UsersAdminDesktop
    

    os.listdir()

    概述

    os.listdir()方法用于获取指定的文件夹下的文件名及文件夹名(不包含子目录)

    语法

    os.listdir(path)
    

    参数

    需要列出的目录路径

    返回值

    一个列表,包含此路径下文件名和文件夹名(不包含子目录)

    实例

    用bat生成的目录树

    C:UsersAdminDesktop	estoswalk
    │  aaa.accdb
    │  b.mxd
    │  
    ├─A
    │      bighead.txt
    │      路径处理.txt
    │      
    ├─ABC
    │  │  12.txt
    │  │  bighead.xlsx
    │  │  
    │  └─D
    │          df.pub
    │          
    └─B
            bighead.docx
    

    代码

    import os
    files=os.listdir(r"C:UsersAdminDesktop	estoswalk")
    for file in files :
        print file
    

    运行结果

    A
    a.txt
    aaa.accdb
    ABC
    B
    b.mxd
    

    os.walk()

    概述

    os.walk()方法用于获取目录树中各目录中的文件名

    语法

    os.walk(top,topdown,onerror,followlinks)
    

    参数

    后面三个参数无需深入了解。

    top:目录路径,比选参数

    topdown:可选,默认为True,目录自上而下生成,若为False,目录自下而上生成

    onerror:可选,是一个函数,调用时有一个参数,一个OSError。报告错误后继续walk或者跑出exception终止walk

    followlinks:可选,默认为False,不访问链接文件

    返回值

    一个三元组 (root,dirs,files)

    root:一个字符串,当前正在访问的文件夹的路径

    dirs:一个列表,该文件夹下所有的目录名称(不包括子目录)

    files:一个列表,该文件夹下所有的文件名(不包括子目录)

    注:os.walk 的返回值是一个生成器(generator),也就是说我们需要不断的遍历它,来获得所有的内容。每次遍历的对象都是返回的是一个三元组(root,dirs,files)

    实例

    用bat生成的目录树

    C:UsersAdminDesktop	estoswalk
    │  aaa.accdb
    │  b.mxd
    │  
    ├─A
    │      bighead.txt
    │      路径处理.txt
    │      
    ├─ABC
    │  │  12.txt
    │  │  bighead.xlsx
    │  │  
    │  └─D
    │          df.pub
    │          
    └─B
            bighead.docx
    

    代码

    import os
    path = r"C:UsersAdminDesktop	estoswalk"
    walk = os.walk(path,topdown=False) #自下而上,path下最后生成
    for root,dirs,files in walk :
        print "this is root"
        print root
        print "this is dirs"
        for dir in dirs :
            print dir
        print "this is files"
        for file in files :
            print file
        print "
    "
    

    运行结果

    this is root
    C:UsersAdminDesktop	estoswalkA
    this is dirs
    this is files
    bighead.txt
    路径处理.txt
    
    
    this is root
    C:UsersAdminDesktop	estoswalkABCD
    this is dirs
    this is files
    df.pub
    
    
    this is root
    C:UsersAdminDesktop	estoswalkABC
    this is dirs
    D
    this is files
    12.txt
    bighead.xlsx
    
    
    this is root
    C:UsersAdminDesktop	estoswalkB
    this is dirs
    this is files
    bighead.docx
    
    
    this is root
    C:UsersAdminDesktop	estoswalk
    this is dirs
    A
    ABC
    B
    this is files
    a.txt
    aaa.accdb
    b.mxd
    

    arcpy.da.Walk()

    概述

    Python os 模块包括 os.walk 函数,可用于遍历目录树并查找数据。os.walk 基于文件,不识别地理数据要素类、表或栅格等数据库内容。arcpy.da.Walk 可用于为数据创建目录。

    注意:Walk 函数在 ArcGIS 10.1 Service Pack 1 及以后版本中可用。

    语法

    Walk (top, {topdown}, {onerror}, {followlinks}, {datatype}, {type})
    

    参数

    前四个参数不做介绍,与os.walk一致。

    datatype

    限制返回的结果的数据类型。有效数据类型如下:

    Any —返回所有数据类型。相当于使用 None 或跳过此参数。
    CadDrawing
    CadastralFabric
    Container
    FeatureClass
    FeatureDataset
    GeometricNetwork
    LasDataset
    Layer
    Locator
    Map
    MosaicDataset
    NetworkDataset
    PlanarGraph
    RasterCatalog
    RasterDataset
    RelationshipClass
    RepresentationClass
    Style
    Table
    Terrain
    Text
    Tin
    Tool
    Toolbox
    Topology
    

    如果作为列表或组进行输入,则支持多个数据类型

    for dirpath, dirnames, filenames in arcpy.da.Walk(workspace,datatype=['MosaicDataset', 'RasterDataset']):
    

    type

    可按类型进一步限制要素和栅格数据类型。默认值为 None。

    All —返回所有类型。相当于使用 None 或跳过此参数。
    Any —返回所有类型。相当于使用 None 或跳过此参数。
    

    有效要素类型如下:

    Multipatch — 仅返回多面体要素类。
    Multipoint —仅返回多点要素类。
    Point —仅返回点要素类。
    Polygon —仅返回面要素类。
    Polyline —仅返回折线要素类。
    

    有效栅格类型为:

    BIL — Esri 波段按行交叉格式文件
    BIP — Esri 波段按像元交叉格式文件
    BMP — 位图图形栅格数据集格式
    BSQ — Esri 波段顺序格式文件
    DAT — ENVI DAT 文件
    GIF — 栅格数据集的图形交换格式
    GRID — Esri Grid 栅格数据集格式
    IMG — ERDAS IMAGINE 栅格数据格式
    JP2 — JPEG 2000 栅格数据集格式
    JPG — 联合图像专家组栅格数据集格式
    PNG — 可移植网络图形栅格数据集格式
    TIF — 栅格数据集的标记图像文件格式
    

    如果作为列表或组进行输入,则支持多个数据类型。

    for dirpath, dirnames, filenames in arcpy.da.Walk(workspace,datatype='FeatureClass', type=['Polygon', 'Polyline']):
    

    返回值

    与os.walk基本一致。

    filenames列表内对于Arcgis可以识别的数据类型,只会生成主文件名,如shp文件只会生成*.shp,而不会生成其附带文件。无法识别的则不会生成,如word文件。

    实例

    目录树

    C:UsersAdminDesktop	estdawalk
        Bighead.dbf
        bighead.docx
        Bighead.shp
        Bighead.shx
        New_Shapefile.dbf
        New_Shapefile.shp
        New_Shapefile.shx
    

    代码

    import arcpy,os
    walk = arcpy.da.Walk(r"C:UsersAdminDesktop	estdawalk")
    for dirpath,dirnames,filenames in walk :
        for filename in filenames :
            print filename
            print os.path.join(dirpath,filename)
    

    运行结果

    Bighead.shp
    C:UsersAdminDesktop	estdawalkBighead.shp
    New_Shapefile.shp
    C:UsersAdminDesktop	estdawalkNew_Shapefile.shp
    

    限定type

    代码

    import arcpy,os
    walk = arcpy.da.Walk(r"C:UsersAdminDesktop	estdawalk",type="Point")
    for dirpath,dirnames,filenames in walk :
        for filename in filenames :
            print filename
            print os.path.join(dirpath,filename)
    

    运行结果

    New_Shapefile.shp
    C:UsersAdminDesktop	estdawalkNew_Shapefile.shp
    
  • 相关阅读:
    Mybaits 源码解析 (十二)----- Mybatis的事务如何被Spring管理?Mybatis和Spring事务中用的Connection是同一个吗?
    Mybaits 源码解析 (十一)----- @MapperScan将Mapper接口生成代理注入到Spring-静态代理和动态代理结合使用
    Mybaits 源码解析 (十)----- Spring-Mybatis框架使用与源码解析
    Mybaits 源码解析 (九)----- 一级缓存和二级缓存源码分析
    Mybaits 源码解析 (八)----- 结果集 ResultSet 自动映射成实体类对象(上篇)
    Mybaits 源码解析 (七)----- Select 语句的执行过程分析(下篇)
    Mybaits 源码解析 (六)----- Select 语句的执行过程分析(上篇)
    Mybaits 源码解析 (五)----- Mapper接口底层原理(为什么Mapper不用写实现类就能访问到数据库?)
    Mybaits 源码解析 (四)----- SqlSession的创建过程
    Mybaits 源码解析 (三)----- Mapper映射的解析过程
  • 原文地址:https://www.cnblogs.com/bigmonk/p/12363509.html
Copyright © 2011-2022 走看看