如何将文件夹名声明为glob

2024-09-29 21:54:17 发布

您现在位置:Python中文网/ 问答频道 /正文

下面的代码展示了第一步,我必须将output文件夹声明为global,这样以后的输出也可以保存在其中。现在我在输出文件夹字符串r'optfile/ras1'处得到一个错误。如果您能帮助您正确地将文件存储在输出文件夹中并将其声明为全局文件,将不胜感激。在

import arcpy
import os
import pythonaddins

from datetime import datetime

now = datetime.now()
month = now.month
year = now.year

optfile = "C:/temp/"+str(year)+"_"+str(month)

class DrawRectangle(object):
"""Implementation for rectangle_addin.tool (Tool)"""
    def __init__(self):
        self.enabled = True
        self.cursor = 1
        self.shape = 'Rectangle'
        os.makedirs(optfile)        

    def onRectangle(self, rectangle_geometry):
    """Occurs when the rectangle is drawn and the mouse button is released.
        The rectangle is a extent object."""

        extent = rectangle_geometry
        arcpy.Clip_management(r'D:/test', "%f %f %f %f" %(extent.XMin, extent.YMin, extent.XMax, extent.YMax), r'optfile/ras1', "#", "#", "NONE")
        arcpy.RefreshActiveView()

Tags: 文件importself文件夹声明datetimeisyear
1条回答
网友
1楼 · 发布于 2024-09-29 21:54:17

我认为你的意思是值r'optfile/ras1'没有使用你的optfile变量。这是因为Python不会神奇地读懂你的思想,也不会替换恰好与变量名匹配的部分字符串。在

您必须显式地使用optfile变量,方法是将它与/ras1部分串联起来:

    arcpy.Clip_management(
        r'D:/test',
        "%f %f %f %f" %(extent.XMin, extent.YMin, extent.XMax, extent.YMax),
        optfile + '/ras1', "#", "#", "NONE")

或者,更好的方法是使用os.path.join()函数为您处理路径分隔符:

^{pr2}$

请注意,您的问题与全局变量无关;这适用于您想要连接的变量来自何处。在

相关问题 更多 >

    热门问题