Top of the morning ladies and gents,
I must apologise to have to trouble you with what might appear a small query, which I seem unable to resolve; let me explain; I am trying to pass arguments to a Python script from a WFP C# based GUI application with the object being setting the path and pattern values in the Python via the searchWrapper in C#; as the code snippets might indicate, the tool works fine with a small exception, where if the path or pattern values are preset in Python, all is well, however, if ones wishes to set the path or pattern values via the searchWrapper, then there arises the glitch, and ergo my seeking your input;
C# code snippet:
public class searchWrapper
{
public List<string> searchQuery(List<string> results, string pyCmd, string path, string pattern)
{
string err = null;
ProcessStartInfo psi = new ProcessStartInfo(@"python");
psi.UseShellExecute = false;
psi.RedirectStandardError = true;
psi.RedirectStandardInput = true;
psi.RedirectStandardOutput = true;
psi.CreateNoWindow = true;
psi.WindowStyle = ProcessWindowStyle.Normal;
psi.Arguments += pyCmd;
List<string> resultOutput = new List<string>();
try
{
using (Process pr = Process.Start(psi))
{
Thread thread = new Thread(() => pr.WaitForExit());
thread.Start();
using (StreamReader rd = pr.StandardOutput)
{
string line = rd.ReadToEnd();
results.Add(line);
}
}
}
catch(Exception ex)
{
err += ex.Message + "\n" + ex.Source;
}
return results;
}
}
A simple Python code snippet:
#ep.
path = r" c:\users\[username]\appdata\local\microsoft\windows\temporary internet files"
pattern = "*.exe"
def getPatterns(path, patterns):
try:
if path != None:
print path, "\n"
for root, dirs, files in os.walk(path):
for fn in fnmatch.filter(files, pattern):
print(os.path.join(root, "\t\t", fn))
print "\n"
else:
print "\npath could not be null;"
except e:
print"\nOoops ...\t", str(e)
#endp.
Put it differently, is it possible to pass the 'path' and 'pattern' arguments to the function via the searchWrapper?
And for disclosure, whilst I like Python, I certainly am not a Pythonian, but do prefer this approach of accessing Python code over IronPython, for which others might opt.
I do thank you, and my apologies should the query is found unclear;
Much obliged,
Cheers,
Gashan,
Out of curiosity, what is the value of sys.argv in the Python script when you call it from C#? Is it an empty array? Or are all the arguments pushed together into one value (e.g. sys.argv[1])?