python - 重新运行程序时出现问题

标签 python file python-2.5

我有一个相当简单的 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). 

最佳答案

我看不出错误消息 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"

更新

用谷歌搜索错误消息中的前几个单词(总是一个好主意!)会显示:troubleshooting geoprocessing errors它提供以下信息:

geoprocessing errors that occur when reading or writing ArcSDE/DBMS data receive a generic 'catch-all' error message, such as error 00210 when writing output

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

关于python - 重新运行程序时出现问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1646747/

相关文章:

python - 将 TextBlob 情感分析结果拆分为两个单独的列 - Python Pandas

python - 在 Python 中切片文件

php - 使用 curl 上传多个文件

java - 如何从文件中只读取整数?

python - 强制删除文件,如 "rm -f"或强制取消文件路径与目录的链接

python - 如何重命名所有文件夹?

python - 信号压缩

python - Pandas 风格 : Draw borders over whole row including the multiindex

python - Python中如何判断一个多边形是否在另一个多边形内?

Python:如何在 pandas 0.9.0 上开发 Between_time 类似的方法?