【问题标题】:Need code to return directory name only只需要代码返回目录名称
【发布时间】:2010-09-20 18:42:56
【问题描述】:

我是一个python新手,已经获得了一个让用户输入目录的脚本 shapefile 所在的位置(例如,c:\programfiles\shapefiles)。然后它在每个 shapefile 中创建一个字段,并添加输入的目录路径和 shapefile 名称(例如,c:\programfiles\shapefiles\name.shp)。我想只用 目录名称(例如 shapefiles)。我知道有一个命令可以拆分目录名称,但是如何将基本名称作为函数返回?提前致谢。

import sys, string, os, arcgisscripting
gp = arcgisscripting.create()

# this is the directory user must specify
gp.workspace = sys.argv[1]
# declare the given workspace so we can use it in the update field process
direct = gp.workspace
try:
    fcs = gp.ListFeatureClasses("*", "all")
    fcs.reset()
    fc = fcs.Next()


    while fc:
        fields = gp.ListFields(fc, "Airport")
        field_found = fields.Next()
        # check if the field allready exist.
        if field_found:
            gp.AddMessage("Field %s found in %s and i am going to delete it" % ("Airport", fc))
            # delete the "SHP_DIR" field
            gp.DeleteField_management(fc, "Airport")
            gp.AddMessage("Field %s deleted from %s" % ("Airport", fc))
            # add it back
            gp.AddField_management (fc, "Airport", "text", "", "", "50")
            gp.AddMessage("Field %s added to %s" % ("Airport", fc))
            # calculate the field passing the directory and the filename
            gp.CalculateField_management (fc, "Airport", '"' + direct + '\\' + fc + '"')
            fc = fcs.Next()


        else:
            gp.addMessage(" layer %s has been found and there is no Airport" % (fc))
        # Create the new field
            gp.AddField_management (fc, "Airport", "text", "", "", "50")
            gp.AddMessage("Field %s added to %s" % ("Airport", fc))

        # Apply the directory and filename to all entries       
            gp.CalculateField_management (fc, "Airport", '"' + direct + '\\' + fc + '"')
            fc = fcs.Next()
        gp.AddMessage("field has been added successfully")
        # Remove directory

except:
 mes = gp.GetMessages ()
 gp.AddMessage(mes)

【问题讨论】:

    标签: python file split


    【解决方案1】:

    相关函数:http://docs.python.org/library/os.path.html

    对于包含父路径(如果可用)的目录名:

    os.path.dirname(your_full_filename)
    

    对于包含绝对父路径的目录名:

    os.path.dirname(os.path.abspath(your_full_filename))
    

    只是目录名:

    os.path.split(os.path.dirname(your_full_filename))[-1]
    

    【讨论】:

    • 不按照他的要求做,这会返回他想要命名的文件夹的完整路径。
    • @James: 啊...我一定误解了这个问题,因为我们需要添加一个os.path.split(...)[-1]
    【解决方案2】:
    import os    
    
    def getparentdirname(path):
        if os.path.isfile(path):
            dirname = os.path.dirname(path)
            return os.path.basename(dirname[0:-1] if dirname.endswith("\\") else dirname)
        else:
            return os.path.basename(path[0:-1] if dirname.endswith("\\") else path)
    

    这应该可以解决问题 - 尽管它确实依赖于您计算机上的路径(以便 os.path.isfile 可以检查路径是文件还是目录)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-03-27
      • 1970-01-01
      • 2021-11-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多