我有一个 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/