在CMD Works中运行脚本但在C#python脚本中使用Process.Start()运行时失败

时间:2015-03-17 21:26:05

标签: c# python regex process

我有一个python脚本, changeDates.py ,它成功运行在cmd中由命令启动的C#程序之外:

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中的正则表达式会突然导致错误? 我该如何解决这个问题?

1 个答案:

答案 0 :(得分:1)

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