python命令提示符窗口下如何运行python脚本解析

发布时间:2020-11-03 20:00:46 作者:Leah
来源:亿速云 阅读:238

python命令提示符窗口下如何运行python脚本解析?很多新手对此不是很清楚,为了帮助大家解决这个难题,下面小编将为大家详细讲解,有这方面需求的人可以来学习下,希望你能有所收获。

方式一:脚本中指定好相关参数设置

python命令提示符窗口下如何运行python脚本解析

import arcpy
import json
import sys
import os
import argparse
import re

result = ""
jsontext = {'success': {}, 'msg': ''}


def checkSavedAprxDatasource(output_aprx_path, mapName, targetDb):
  check_result = True
  desc = arcpy.Describe(targetDb)

  saved_aprx = arcpy.mp.ArcGISProject(output_aprx_path)
  for saved_map in saved_aprx.listMaps():
    if saved_map.name == mapName:
      for lyr in saved_map.listLayers():
        if lyr.isFeatureLayer:
          # print(lyr.connectionProperties)
          if lyr.connectionProperties['workspace_factory'].upper() == 'SDE'.upper(
          ) and lyr.connectionProperties['connection_info']['instance'] == desc.connectionProperties.instance:
            pass
          else:
            check_result = False

  return check_result


if __name__ == '__main__':
  try:

    # linux
    # aprxPath = str(sys.argv[1])
    # mapName = str(sys.argv[2])
    # sourceDb = str(sys.argv[3])
    # targetDb = str(sys.argv[4])
    # output_aprx_path = str(sys.argv[5])

    # local pc
    aprxPath = r'D:\rvt\testaprx\testaprx.aprx'
    mapName = 'Map'
    sourceDb = r'D:\rvt\testaprx\Data.gdb'
    targetDb = r'D:\rvt\testaprx\testsde.sde'
    output_aprx_path = r'D:\rvt\testaprx\output\export5.aprx'

    aprx = arcpy.mp.ArcGISProject(aprxPath)
    for m in aprx.listMaps():
      if m.name == mapName:
        m.updateConnectionProperties(sourceDb, targetDb)

    if os.path.exists(output_aprx_path):
      os.remove(output_aprx_path)
    aprx.saveACopy(output_aprx_path)

    checkResult = checkSavedAprxDatasource(
      output_aprx_path, mapName, targetDb)
    if checkResult:
      jsontext['success'] = True
      result = json.dumps(jsontext)
      sys.stdout.write(result)
    else:
      jsontext['success'] = False
      jsontext['msg'] = 'Failed to replace data source'
      result = json.dumps(jsontext)
      sys.stdout.write(result)

  except Exception as e:
    jsontext['success'] = False
    jsontext['msg'] = e.args
    result = json.dumps(jsontext)
    sys.stdout.write(result)

方式二:使用sys.argv[ ]的形式设置相关参数

(arcgispro-py3) D:\rvt\testaprx>python test.py D:\rvt\testaprx\testaprx.aprx Map D:\rvt\testaprx\Data.gdb D:\rvt\testaprx\testsde.sde D:\rvt\testaprx\output\export5.aprx

python命令提示符窗口下如何运行python脚本解析

import arcpy
import json
import sys
import os
import argparse
import re

result = ""
jsontext = {'success': {}, 'msg': ''}


def checkSavedAprxDatasource(output_aprx_path, mapName, targetDb):
  check_result = True
  desc = arcpy.Describe(targetDb)


  saved_aprx = arcpy.mp.ArcGISProject(output_aprx_path)
  for saved_map in saved_aprx.listMaps():
    if saved_map.name == mapName:
      for lyr in saved_map.listLayers():
        if lyr.isFeatureLayer:
          # print(lyr.connectionProperties)
          if lyr.connectionProperties['workspace_factory'].upper() == 'SDE'.upper(
          ) and lyr.connectionProperties['connection_info']['instance'] == desc.connectionProperties.instance:
            pass
          else:
            check_result = False

  return check_result


if __name__ == '__main__':
  try:

    # linux
    aprxPath = str(sys.argv[1])
    mapName = str(sys.argv[2])
    sourceDb = str(sys.argv[3])
    targetDb = str(sys.argv[4])
    output_aprx_path = str(sys.argv[5])
    print("aprx路径: "+aprxPath)
    print("地图视图的名称: "+mapName)
    print("当前的数据源路径: "+sourceDb)
    print("目标数据源路径: "+targetDb)
    print("aprx另存为路径: "+output_aprx_path)

    # local pc
    # aprxPath = r'D:\rvt\testaprx\testaprx.aprx'
    # mapName = 'Map'
    # sourceDb = r'D:\rvt\testaprx\Data.gdb'
    # targetDb = r'D:\rvt\testaprx\testsde.sde'
    # output_aprx_path = r'D:\rvt\testaprx\output\export5.aprx'

    aprx = arcpy.mp.ArcGISProject(aprxPath)
    for m in aprx.listMaps():
      if m.name == mapName:
        m.updateConnectionProperties(sourceDb, targetDb)

    if os.path.exists(output_aprx_path):
      os.remove(output_aprx_path)
    aprx.saveACopy(output_aprx_path)

    checkResult = checkSavedAprxDatasource(
      output_aprx_path, mapName, targetDb)
    if checkResult:
      jsontext['success'] = True
      result = json.dumps(jsontext)
      sys.stdout.write(result)
    else:
      jsontext['success'] = False
      jsontext['msg'] = 'Failed to replace data source'
      result = json.dumps(jsontext)
      sys.stdout.write(result)

  except Exception as e:
    jsontext['success'] = False
    jsontext['msg'] = e.args
    result = json.dumps(jsontext)
    sys.stdout.write(result)

看完上述内容是否对您有帮助呢?如果还想对相关知识有进一步的了解或阅读更多相关文章,请关注亿速云行业资讯频道,感谢您对亿速云的支持。

推荐阅读:
  1. 运行python脚本的方法是什么
  2. python脚本如何在docker上运行

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python 命令提示符 命令提示

上一篇:详解Linux输入子系统框架的原理

下一篇:Ubuntu12.04如何实现建立内核树

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》