c# - 在 CMD 中运行脚本可以工作,但是在 C# python 脚本中使用 Process.Start() 运行时失败

标签 c# python regex process

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

python changeDates.py 路径/到/文件夹 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中的正则表达式会突然出错? 我该如何解决这个问题?

最佳答案

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

关于c# - 在 CMD 中运行脚本可以工作,但是在 C# python 脚本中使用 Process.Start() 运行时失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29110022/

相关文章:

c# - 模型或 Controller 中的 Asp.net mvc 下拉代码

python - 用户输入了多少个数字和字母?

javascript - 如何在正则表达式中跳过换行并比较数学?

javascript - 扩展正则表达式

regex - 如何明智地结合 shingles 和 edgeNgram 来提供灵活的全文搜索?

c# - 在 C# 中将数据导入 Cassandra Cluster 的最佳方法

c# - 使用 LINQ 将数据从一个列表复制到另一个列表

具有标志属性的 C# 枚举

python - "from . import views": Unresolved import

python - 如何在 python 包装中使用 unicode 字符串为 c++ 类使用 cython?