【问题标题】:Running Script in CMD Works but when ran using Process.Start() in C# python script fails在 CMD 中运行脚本 Works 但在 C# python 脚本中使用 Process.Start() 运行时失败
【发布时间】:2015-03-17 21:26:05
【问题描述】:

我有一个 python 脚本,changeDates.py,它成功地在一个 C# 程序之外运行,该程序通过以下命令在 cmd 中启动:

python changeDates.py path/to/folder numberOfMonths numberOfWeeks testSetsToCheck

这些参数都是字符串。 numberOfMonths 和 numberOfWeeks 作为字符串传递给 python 脚本,然后在脚本内部转换为 int。

但如果我要使用以下命令运行相同的命令:

private void run_CMD(string cmd, string args, bool messageBox)
        {
            try
            {
                Console.WriteLine(cmd);
                Console.WriteLine(args);
                ProcessStartInfo start = new ProcessStartInfo();
                start.FileName = cmd;
                start.Arguments = args;
                start.UseShellExecute = false;
                start.RedirectStandardOutput = true;
                using (Process process = Process.Start(start))
                {
                    using (StreamReader reader = process.StandardOutput)
                    {
                        string result = reader.ReadToEnd();
                        Console.Write(result);
                    }
                }
            }
            catch (Exception ex)
            {
                Console.WriteLine("Error while trying to check package dates: \n" + ex);
                Logger.Write(Logger.Level.ERROR, "Error while trying to check package dates: \n" + ex);
            }
        }

脚本启动并输出以下错误:

 C:\Users\bblashko\Documents\VisualStudio2012\Projects\Athena_Test_Automation_Fr
amework\Athena_Test_Automation_Framework\scripts\changeDates.py C:\Users\bblashk
o\Documents\VisualStudio2012\Projects\Athena_Test_Automation_Framework\Athena_Te
st_Automation_Framework\Test_Cases 6 1 00100
Traceback (most recent call last):
  File "C:\Users\bblashko\Documents\VisualStudio2012\Projects\Athena_Test_Automa
tion_Framework\Athena_Test_Automation_Framework\scripts\changeDates.py", line 51
0, in <module>
    allFiles = checkContent(content, subDir, int(sys.argv[2]), int(sys.argv[3]))

  File "C:\Users\bblashko\Documents\VisualStudio2012\Projects\Athena_Test_Automa
tion_Framework\Athena_Test_Automation_Framework\scripts\changeDates.py", line 47
, in checkContent
    checkXLSX(f, subDir, numberOfMonths, numberOfWeeks)
  File "C:\Users\bblashko\Documents\VisualStudio2012\Projects\Athena_Test_Automa
tion_Framework\Athena_Test_Automation_Framework\scripts\changeDates.py", line 85
, in checkXLSX
    changeDate = checkXLSXDates(salesStartDate, pubDate, type, todaysDate, check
Date)
  File "C:\Users\bblashko\Documents\VisualStudio2012\Projects\Athena_Test_Automa
tion_Framework\Athena_Test_Automation_Framework\scripts\changeDates.py", line 15
7, in checkXLSXDates
    if(re.search("(\w\w)/(\w\w)/(\w\w\w\w)", salesStartDate) and re.search("(\w\
w)/(\w\w)/(\w\w\w\w)", pubDate)):
  File "C:\Python34\lib\re.py", line 166, in search
    return _compile(pattern, flags).search(string)
TypeError: expected string or buffer

为什么python中的正则表达式会突然出错? 我该如何解决这个问题?

【问题讨论】:

  • 错误的论点?也许你引用了错误的东西——在 Windows 中,引号是特殊的、特殊的东西。不同的权限?在 VS 中,您可能具有管理权限,而在外部您很可能没有。我的意思是我们只是在这里猜测,需要更多信息!
  • 我不知道如何获得更多信息...

标签: c# python regex process


【解决方案1】:

您传递给re.search 函数的string 参数是一个python 模块,当您以这种方式执行python 代码时,变量string 没有正确分配!所以,首先,不要使用python关键字和内置名称作为你的变量名,为了摆脱这种情况,你需要检查你在代码中分配string的方式!

【讨论】:

  • 我不明白这可能是 python 代码的问题。因为我能够在 cmd 和 java 中成功运行 python 脚本。不过我会检查的。
  • @user2853442 我说过,当您使用 c# 执行代码时,string 无法正确分配!您是否使用 c# 将任何参数传递给您的脚本?
  • 对不起,我误会了。我将使用参数编辑我的问题。
  • 我确实使用了参数,所有这些参数都作为字符串传递给 python 脚本。
  • @user2853442 我不熟悉c#,你需要检查你将numberOfMonths numberOfWeeks testSetsToCheck传递给你的python脚本的方式!
猜你喜欢
  • 2015-04-06
  • 1970-01-01
  • 2022-09-30
  • 1970-01-01
  • 1970-01-01
  • 2015-09-10
  • 1970-01-01
  • 2017-12-05
  • 1970-01-01
相关资源
最近更新 更多