0
がどのように私はC#のがこののpython 2.7のコードを実行してくださいparamsは(ファイル名はmyPythonScript.py
です):上記のコード実行のPython 2.7 sriptが
import argparse
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument(
'-sd', '-start_date', help='start date to download data')
parser.add_argument(
'-ed', '-end_date', help='end date to download data')
args = parser.parse_args()
#print(args.accumulate(args.start_date))
print(args.sd, args.ed)
は、コマンドラインのparamsように2つの日付を取り、それを示していユーザに提供する。私はC#からプロセスとして実行したいです。 このC#コードを使用すると、paramsなしでもスクリプトが実行されます。しかし、paramsを追加すると、pythonファイルを見つけることができません。どうして?これを解決するには?
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = @"C:\Anaconda2\python.exe";
// path to my python script
string appEXE = AppDomain.CurrentDomain.BaseDirectory;
// this scripts runs without params
python_script_name = @"myPythonScript.py -sd 01/01/2015 -ed 05/09/2017";
startInfo.Arguments="\""+appEXE+ "Python\\"+ python_script_name + "\"";
startInfo.UseShellExecute = false;
startInfo.RedirectStandardOutput = true;
startInfo.CreateNoWindow = true;
startInfo.RedirectStandardError = true;
using (Process process = Process.Start(startInfo))
{
using (StreamReader reader = process.StandardOutput)
{
string result = reader.ReadToEnd();
//MessageBox.Show("Normal results"+result);
Debug.WriteLine(result);
}
process.WaitForExit();
// This will show error: no such file or directory
MessageBox.Show("Errors"+process.StandardError.ReadToEnd());
GC.Collect();
まず、パスを連結するときはいつでも、 'System.IO.Path.Combine()'を使ってみてください。 Path.Combine(AppDomain.CurrentDomain.BaseDirectory、 "Python"、 "myPythonScript.py")を使用します。問題があるかどうかわかりませんが、読みやすくするために役立ちます。 –