重新运行程序时出现问题
我有一个相当简单的 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).
I have a fairly simple python loop that calls a few functions, and writes the output to a file. To do this is creates a folder, and saves the file in this folder.
When I run the program the first time with a unique file name, it runs fine. However, if I try to run it again, it will not work and I do not understand why. I am quite certain that it is not a problem of overwriting the file, as I delete the folder before re-running, and this is the only place that the file is stored. Is there a concept that I am mis-understanding?
The problematic file is 'buff1.shp'. I am using Python 2.5 to run some analysis in ArcGIS
Thanks for any advice (including suggestions about how to improve my coding style). One other note is that my loops currently only use one value as I am testing this at the moment.
# 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"
(To clarify this question I edited a few parts that did not make sense without the rest of the code. The error still remains in the program.)
Error message:
ExecuteError: ERROR 000210: Cannot create output C:\LEED\Cities_20_Oct\services\results\buff1.shp Failed to execute (Buffer).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我看不出错误消息 blahblah\buff1.shp 中的文件名是如何从您的代码中产生的。
应该产生
blahblah\buff0.shp
而不是blahblah\buff1.shp
...我强烈建议您显示的代码应该是您实际运行的代码。在gp.Buffer_analysis()
调用之前添加打印语句以显示 i 和 repr(buffer_shp) 的值。显示所有打印结果。另外,注释
#Loop through a range of buffers (1 ->100)
表示您希望从 1 而不是 0 开始。如果注释与代码匹配,它会对(您)有很大帮助。不要重复自己;而不是
这样做:
您可能想养成使用 os.path.join() 构建所有路径的习惯。
您需要在循环外部调用 os.mkdir() ,即每次运行脚本时调用一次,而不是每次在内循环中调用一次。
不使用这些语句的结果:
更新
使用错误消息中的前几个单词进行谷歌搜索(总是一个好主意!)会显示以下内容:对地理处理错误进行故障排除,它提供以下信息:
这继续建议了一些确定您的确切问题是什么的方法。如果这对您没有帮助,您可能想尝试在相关的 ESRI 论坛或 GIS StackExchange 上提问。
I can't see how the file name in the error message blahblah\buff1.shp can arise from your code.
should produce
blahblah\buff0.shp
notblahblah\buff1.shp
... I strongly suggest that the code you display should be the code that you actually ran. Throw in a print statement just before thegp.Buffer_analysis()
call to show the value of i and repr(buffer_shp). Show all print results.Also the comment
#Loop through a range of buffers (1 ->100)
indicates you want to start at 1, not 0. It helps (you) greatly if the comments match the code.Don't repeat yourself; instead of
do this:
You might like to get into the habit of constructing all paths using os.path.join().
You need to take the call to
os.mkdir()
outside the loops i.e. do it once per run of the script, not once each time round the inner loop.The results of these statements are not used:
Update
Googling with the first few words in your error message (always a good idea!) brings up this: troubleshooting geoprocessing errors which provides the following information:
This goes on to suggest some ways of determining what your exact problem is. If that doesn't help you, you might like to try asking in the relevant ESRI forum or on GIS StackExchange.
我看到这是一个 3 年前的帖子,但其他人会添加:
当我生成与 Arc 一起使用的 python 脚本时,我总是在导入后立即包含:
您还提到您删除了“文件夹”?。这将是您目录的一部分,我看不到您在脚本中的位置创建目录。您可能想要清除该文件夹,而不是删除它(也许您的意思是删除该文件)。
锦江华
I see this is a 3 year old posting, but for others will add:
As I generate python script to work with Arc, I always include right after my import:
Also you mentioned you delete your "folder"?. That would be part of your directory, and I do not see where you are creating a directory in the script. You would want to clear the folder, not delete it (maybe you meant you delete the file though).
JJH
我很想再次查看
path = "C:\LEED\Cities_20_Oct\services\"
你肯定想要双前斜杠,而不是双反斜杠?
I'd be tempted to look again at
path = "C:\LEED\Cities_20_Oct\services\"
Surely you want double front slashes, not double back slashes?