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 628469
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T19:38:25+00:00 2026-05-13T19:38:25+00:00

I’ve written a simple program that captures and executes command line Python scripts, but

  • 0

I’ve written a simple program that captures and executes command line Python scripts, but there is a problem. The text passed to a Python input function isn’t written to my program despite my program capturing stdout.

For example:
The Python script:

import sys

print("Hello, World!")
x = input("Please enter a number: ")
print(x)

print("This work?")

Would write “Hello, World!” then stop. When I pass it a number it would continue on writing “Please enter a number: 3”. What is going on? Any solutions? My C# is as follows:

public partial class PyCon : Window
{
        public string strPythonPath;
        public string strFile;
        public string strArguments;
        private StreamWriter sw;

        public PyCon(string pythonpath, string file, string args)
        {
            strPythonPath = pythonpath;
            strFile = file;
            strArguments = args;

            InitializeComponent();

            Process p = new Process();

            p.StartInfo.FileName = strPythonPath;
            p.StartInfo.Arguments = "\"" + strFile + "\" " + strArguments;

            p.StartInfo.UseShellExecute = false;
            p.StartInfo.CreateNoWindow = true;

            p.StartInfo.RedirectStandardInput = true;
            p.StartInfo.RedirectStandardOutput = true;
            p.StartInfo.RedirectStandardError = true;

            p.OutputDataReceived += new DataReceivedEventHandler(p_OutputDataReceived);
            p.ErrorDataReceived += new DataReceivedEventHandler(p_ErrorDataReceived);

            p.Start();
            p.BeginOutputReadLine();
            p.BeginErrorReadLine();
            sw = p.StandardInput;
        }

        private void p_OutputDataReceived(object sendingProcess, DataReceivedEventArgs received) {
            if (!String.IsNullOrEmpty(received.Data)) {
                AppendConsole(received.Data);
            }
        }

        private void p_ErrorDataReceived(object sendingProcess, DataReceivedEventArgs received) {
            if (!String.IsNullOrEmpty(received.Data)) {
                AppendConsole(received.Data);
            }
        }

        private void AppendConsole(string message) {
            if (!txtConsole.Dispatcher.CheckAccess()) {
                txtConsole.Dispatcher.Invoke(DispatcherPriority.Normal, (System.Windows.Forms.MethodInvoker)delegate() { txtConsole.AppendText(message + "\n"); });
            } else {
                //Format text
                message = message.Replace("\n", Environment.NewLine);

                txtConsole.AppendText(message + "\n");   
            }
        }

        private void txtInput_KeyUp(object sender, KeyEventArgs e) {
            if (e.Key != Key.Enter) return;

            sw.WriteLine(txtInput.Text);

            txtInput.Text = "";


        }
    }

Edit: After a lot of research and help from this thread, I’ve come to the conclusion that the problem is with the Python input command not calling the C# DataReceivedEventHandler. There may not be a solution to this besides scripting changes. If that is the case, I’ll make the answer containing those changes as accepted. Thanks for the help, guys!

  • 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-05-13T19:38:26+00:00Added an answer on May 13, 2026 at 7:38 pm

    Smells like the Python i/o is line buffered, i.e. waits for a CRLF then sends a whole line at once. You could try turning that off (python -u myscript.py, or set the PYTHONUNBUFFERED environment variable) or work around it with something like this:

    print("Hello, World!")
    print("Please enter a number: ")
    x = input()
    print(x)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a French site that I want to parse, but am running into
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm working with an upstream system that sometimes sends me text destined for HTML/XML
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

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.