【问题标题】:pass file path through cmd to python script通过 cmd 将文件路径传递给 python 脚本
【发布时间】:2012-06-29 04:42:57
【问题描述】:

我正在为我的程序使用 vb.net 和 Arcobjects。我正在为 ArcMap 10 创建一个按钮,它将 kml 转换为 lyr 文件。

我在将变量传递到 python 代码时遇到问题。这些变量是文件路径,如果我用 / 而不是 .当变量动态传入时,程序会在路径名中的“/”处中断:

    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 

【问题讨论】:

    标签: python vb.net cmd argv


    【解决方案1】:

    返回的路径包含空格?从您的初始目录看来是这样。
    在这种情况下,传递给脚本的命令参数可能是错误的。

    尽量用双引号将所有内容括起来,并避免直接操作路径。
    请改用 Path.GetDirectoryName()

    Dim args As String         
    args = """" + kmlFile + """ " 
    args = args & """" & Path.GetDirectoryName(kmlFile) & """ test" 
    

    【讨论】:

      【解决方案2】:

      "\" 替换为"\\\"

      有效吗?

      【讨论】:

      • 我用 / Dim args2 替换了 \ As String = args.Replace("\", "/") 编辑了我上面的代码。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-06
      • 1970-01-01
      • 2019-10-22
      • 1970-01-01
      • 2021-09-11
      • 2011-01-23
      相关资源
      最近更新 更多