1

我有一个相当简单的 python 循环,它调用一些函数,并将输出写入文件。为此,创建一个文件夹,并将文件保存在此文件夹中。

当我第一次使用唯一的文件名运行程序时,它运行良好。但是,如果我再次尝试运行它,它将无法正常工作,我不明白为什么。我很确定这不是覆盖文件的问题,因为我在重新运行之前删除了文件夹,这是唯一存储文件的地方。有没有我误解的概念?

有问题的文件是“buff1.shp”。我正在使用 Python 2.5 在 ArcGIS 中运行一些分析

感谢您提供任何建议(包括有关如何改进我的编码风格的建议)。另一个注意事项是我的循环目前只使用一个值,因为我目前正在测试它。

# Import system modules
import sys, string, os, arcgisscripting, shutil

# Create the Geoprocessor object
gp = arcgisscripting.create()

# Load required toolboxes...
gp.AddToolbox("C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Spatial Statistics Tools.tbx")
gp.AddToolbox("C:/Program Files/ArcGIS/ArcToolbox/Toolboxes/Analysis Tools.tbx")

# specify workspace
gp.Workspace = "C:/LEED/Cities_20_Oct/services"
path = "C:\\LEED\\Cities_20_Oct\\services\\"

results = 'results\\' 
os.mkdir( path + results )      
newpath = path + results

# Loop through each file (0 -> 20)
for j in range(0,1):
    in_file = "ser" + str(j) + ".shp"
    in_file_2 = "ser" + str(j) + "_c.shp"
    print "Analyzing " + str(in_file) + " and " + str(in_file_2)    

    #Loop through a range of buffers - in this case, 1,2
    for i in range(1,2):
        print "Buffering....."  

        # Local variables...
        center_services = in_file_2
        buffer_shp = newpath + "buff" + str(i) + ".shp"
        points = in_file_2
        buffered_analysis_count_shp = newpath + "buffered_analysis_count.shp"
        count_txt = newpath + "count.txt"

        # Buffer size
        b_size = 1000 + 1000 * i
        b_size_input = str(b_size) + ' METERS'

        print "Buffer:" + b_size_input + "\n"

        # Process: Buffer...
        gp.Buffer_analysis(center_services, buffer_shp, b_size_input, "FULL", "ROUND", "ALL", "")
        print "over"

(为了澄清这个问题,我编辑了一些没有其余代码就没有意义的部分。错误仍然存​​在于程序中。)

错误信息:

 ExecuteError: ERROR 000210: Cannot create output C:\LEED\Cities_20_Oct\services\results\buff1.shp Failed to execute (Buffer). 
4

3 回答 3

2

我看不出错误消息 blahblah\buff1.shp 中的文件名是如何从您的代码中产生的。

for i in range(0,1):
    buffer_shp = newpath + "buff" + str(i) + ".shp"
    gp.Buffer_analysis(center_services, buffer_shp, etc etc)

blahblah\buff0.shp不应该产生blahblah\buff1.shp...我强烈建议您显示的代码应该是您实际运行的代码。在调用之前添加一个打印语句gp.Buffer_analysis()以显示 i 和 repr(buffer_shp) 的值。显示所有打印结果。

注释还#Loop through a range of buffers (1 ->100)表明您希望从 1 开始,而不是 0。如果注释与代码匹配,它会对(您)有很大帮助。

不要重复自己;代替

    os.mkdir( path + results )      
    newpath = path + results

做这个:

    newpath = path + results # using os.path.join() is even better
    os.mkdir(newpath)      

您可能希望养成使用 os.path.join() 构建所有路径的习惯。

您需要os.mkdir()在循环之外进行调用,即每次运行脚本时调用一次,而不是每次在内循环中调用一次。

不使用这些语句的结果:

    buffered_analysis_count_shp = newpath + "buffered_analysis_count.shp"
    count_txt = newpath + "count.txt"

更新

使用错误消息中的前几个单词进行谷歌搜索(总是一个好主意!)会显示:排除地理处理错误,它提供以下信息:

读取或写入 ArcSDE/DBMS 数据时发生的地理处理错误会收到一般的“catch-all”错误消息,例如写入输出时出现错误 00210

这继续提出一些方法来确定您的确切问题是什么。如果这对您没有帮助,您可能想尝试在相关的 ESRI 论坛或GIS StackExchange上提问。

于 2009-10-29T23:09:24.627 回答
1

我看到这是一个 3 岁的帖子,但对于其他人会添加:

当我生成 python 脚本以使用 Arc 时,我总是在导入后立即包含:

arcpy.env.overwriteOutput=True   # This allows the script to overwrite files.

您还提到您删除了“文件夹”?那将是您目录的一部分,我看不到您在脚本中创建目录的位置。您想清除文件夹,而不是删除它(也许您的意思是删除文件)。

锦江

于 2013-02-27T17:07:41.727 回答
0

我很想再看看

路径 = "C:\LEED\Cities_20_Oct\services\"

当然你想要双前斜线,而不是双反斜线?

于 2010-09-23T18:54:48.063 回答