活动
最新
False
横幅背景图像
开发者活动
上次更新日期 2024年4月23日

管理 Python 脚本

下面的示例说明了如何自动运行在运行时创建的 Python 代码并写入文件日志。 它提供了“ Python 作用域 ”和 “ 运行 Python 脚本”等活动。 您可以在 UiPath.Python.Activities 包中找到这些活动。

注意: 在运行任何 Python 工作流之前,请确保已安装 Python3。

此自动化流程的构建方法如下:

  1. 打开 Studio ,创建一个新“流程”
  2. 工作流设计器中拖动“序列”容器。
  3. 创建新参数:

    参数名称

    方向

    参数类型

    默认值

    in_PythonPath

    输入

    字符串

    添加已安装 Python 的文件夹的路径

  4. 在“ 序列 ”容器内拖动“ If条件”活动。

    • 在“条件”字段中添加表达式“String.IsNullOrEmpty(in_PythonPath) orelse not Directory.Exists(in_PythonPath)”。
  5. 将“序列”容器拖动到“If 条件”活动的“Then”字段中。
  6. 将“ 消息框” 活动拖动到“ 序列 ”容器内。

    • 在“ 属性 ” 面板的“ 标题 ” 字段中添加表达式 "Error!"
    • 在“文本”字段中添加表达式 "Pyhon Path not configured! Please configure path by setting the argument 'in_PythonPath'"
  7. 将“ 终止工作流 ”活动拖动到“ 消息框” 活动下方。

    • 在“ 属性 ” 面板的“ 原因” 字段中添加表达式 "Input Arguments not correct"
  8. 将“ Python 作用域 ”活动拖动到“ If 条件”活动下方。

    • 在“ 属性 ” 面板的“ 路径 ” 字段中添加参数 in_PythonPath
    • 从“ 版本 ” 下拉列表中选择您的 Python 版本(在此示例中为Python_36 )。
  9. 在“ Python 作用域 ”容器内拖动“运行 Python 脚本”活动。

    • 在“ 属性 ”面板的 “代码” 字段中添加以下代码片段:

      String.Format(
      "import sys" + Environment.NewLine _
      + "import os " + Environment.NewLine _
      + "with open('{0}\logs.txt', 'w') as f: " + Environment.NewLine _
      +     "    f.write('Starting script! \n')" + Environment.NewLine  _
      +     "    f.write('Computing!\n')" + Environment.NewLine _
      +     "    f.write('Finishing script! \n')" + Environment.NewLine, _
      Directory.GetCurrentDirectory.Replace("\", "\\"))String.Format(
      "import sys" + Environment.NewLine _
      + "import os " + Environment.NewLine _
      + "with open('{0}\logs.txt', 'w') as f: " + Environment.NewLine _
      +     "    f.write('Starting script! \n')" + Environment.NewLine  _
      +     "    f.write('Computing!\n')" + Environment.NewLine _
      +     "    f.write('Finishing script! \n')" + Environment.NewLine, _
      Directory.GetCurrentDirectory.Replace("\", "\\"))
  10. 运行流程。 自动化运行 Python 代码,并在名为 logs.txt的文件中写入日志。

此页面是否有帮助?

获取您需要的帮助
了解 RPA - 自动化课程
UiPath Community 论坛
Uipath 白色徽标
信任与安全
© 2005-2024 UiPath. All rights reserved.