2012-06-28 107 views
1

我正在使用vb.net和Arcobjects作爲我的程序。我正在爲ArcMap 10創建一個按鈕,該按鈕將將kml轉換爲lyr文件。將文件路徑通過cmd傳遞給python腳本

我有問題將變量傳入Python代碼。這些變量是文件路徑,如果我用/代替硬編碼,它會很好用。當變量動態傳遞中,在路徑名在 「/」 S程序中斷:

Dim Filelocation As OpenFileDialog = New OpenFileDialog() 

    Filelocation.Title = "Please point photo of the owner" 
    Filelocation.InitialDirectory = "B:\GeoSpatialData\Projects\004402 Griffiths\File Structure\Geospatial\GPS\KML" 

    If Filelocation.ShowDialog = DialogResult.OK Then 
     Dim kmlFile As String 
     kmlFile = Filelocation.FileName 

     Dim args As String 
     args = kmlFile & " " & kmlFile.Substring(0, kmlFile.LastIndexOf("\")) & " test" 
     Dim args2 As String = args.Replace("\", "/") 
     Dim procStartInfo As System.Diagnostics.ProcessStartInfo = New System.Diagnostics.ProcessStartInfo("C:\Python26\python", "C:\Users\KJacobsen\kml_to_shp.py " & args2) 

     ' The following commands are needed to redirect the standard output. 
     ' This means that it will be redirected to the Process.StandardOutput StreamReader. 
     procStartInfo.RedirectStandardOutput = True 
     procStartInfo.UseShellExecute = False 
     ' Do not create the black window. 
     procStartInfo.CreateNoWindow = False 

     ' Now you create a process, assign its ProcessStartInfo, and start it. 
     Dim proc As New System.Diagnostics.Process() 
     proc.StartInfo = procStartInfo 
     proc.Start() 
     proc.WaitForExit() 

     ' Get the output into a string. 
     Dim result As String = proc.StandardOutput.ReadToEnd() 
     ' Display the command output. 
     Console.WriteLine(result) 
    End If 
Catch objException As Exception 
    ' Log the exception and errors. 
    Console.WriteLine(objException.Message) 
End Try 

我的Python腳本是這樣的:

import os 
import arcpy 
import sys 
import glob 
arcpy.KMLToLayer_conversion(sys.argv[1],sys.argv[2],sys.argv[3]) 
print 

回答

0

更換"\""\\\"

它工作嗎?

+0

我取代\與/ 昏暗args2作爲字符串= args.Replace( 「\」, 「/」) 編輯我的上述代碼。 – user1488948

+0

如果路徑有「」,則應在路徑參數周圍使用引號。 – Chachi

0

返回的路徑是否包含空格?從你的初始目錄看起來是如此。
在這種情況下,傳遞給腳本的命令參數可能是錯誤的。

嘗試用雙引號括住所有內容,避免直接操作路徑。
使用Path.GetDirectoryName()代替

Dim args As String   
args = """" + kmlFile + """ " 
args = args & """" & Path.GetDirectoryName(kmlFile) & """ test" 
相關問題