Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7821513
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:40:20+00:00 2026-06-02T07:40:20+00:00

I want to run python code from C# through command Prompt.The Code is attached

  • 0

I want to run python code from C# through command Prompt.The Code is attached below

    Process p = new Process();
    p.StartInfo.FileName = "cmd.exe";
    p.StartInfo.WorkingDirectory = @"d:";
    p.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
    p.StartInfo.UseShellExecute = false;
    p.StartInfo.RedirectStandardOutput = true;
    p.StartInfo.RedirectStandardInput = true;

    p.Start();
    p.StandardInput.WriteLine(@"cd D:\python-source\mypgms");
    p.StandardInput.WriteLine(@"main.py -i example-8.xml -o output-8.xml");

    string output = p.StandardOutput.ReadToEnd();
    p.WaitForExit();

    Console.WriteLine("Output:");
    Console.WriteLine(output);

Output :

D:\python-source\mypgms>main.py -i example-8.xml -o output-8.xml

D:\python-source\mypgms>

But nothing happened.Actually main.py is my main program and it takes 2 arguments. one is input xml file and another one is converted output xml file.

But i dont know how to run this python script from C# through command prompt. Please Guide me to get out of this issue…

Thanks & Regards,
P.SARAVANAN

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T07:40:22+00:00Added an answer on June 2, 2026 at 7:40 am

    I think you are mistaken in executing cmd.exe. I’d say you should be executing python.exe, or perhaps executing main.py with UseShellExecute set to true.

    At the moment, your code blocks at p.WaitForExit() because cmd.exe is waiting for your input. You would need to type exit to make cmd.exe terminate. You could add this to your code:

    p.StandardInput.WriteLine(@"exit");
    

    But I would just cut out cmd.exe altogether and call python.exe directly. So far as I can see, cmd.exe is just adding extra complexity for absolutely no benefit.

    I think you need something along these lines:

    var p = new Process();
    p.StartInfo.FileName = @"Python.exe";
    p.StartInfo.Arguments = "main.py input.xml output.xml";
    p.StartInfo.WorkingDirectory = @"D:\python-source \mypgms";
    p.StartInfo.WindowStyle = ProcessWindowStyle.Normal;
    p.StartInfo.UseShellExecute = false;
    p.StartInfo.RedirectStandardOutput = true;
    p.Start();
    p.WaitForExit();
    

    Also the Python script appears to output to a file rather than to stdout. So when you do p.StandardOutput.ReadToEnd() there will be nothing there.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to run a python script from within another. By within I mean
I want to run some Python unit tests from my Maven module. I have
I run the following code from a python interpreter, and expect the insert statement
I want to start, from Python, some other Python code, preferably a function, but
I want to run php or python scripts from eclipse that will create folders
I want to run Python code coverage tests for Google App Engine in Eclipse
I use python -m SimpleHTTPServer when I want to run my web application locally,
I have a python program/file that I want to run repeatedly and calculate the
I have a Iron Python script that I want to run and then have
If I write program in python 2.7 and I want to run another script

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.