-1

I want to take input from the user after running my py script in Windows.

A new cmd should pop up displaying a particular message like "Do you wish to continue [y/n]" and store this value in a variable in the py script and cmd should close automatically after the user provides input. (Using Python 3) Can anyone please help.

aditya
  • 123
  • 2
  • 11
  • Do you mean "x = input("Do you wish to continue [y/n]")"? Or do you want to start a new terminal window? – Bobby Ocean May 29 '20 at 18:56
  • I want to start a new terminal window (in windows) which will close itself after taking input and the main script execution will resume. – aditya May 29 '20 at 19:02

2 Answers2

1

Here's a way:

Lets say you have three files, the first file (first_half.py) is for the first half of your program, the second file (ask.py) is where the user is asked whether or not to continue, and the third file (second_half.py) is for if the user chooses to continue, it gets executed.

In your first_half.py python file:

# Your code
import os
os.system('cmd /k "python ask.py"')

In a ask.py:

a = input("Do you wish to continue? ")
line = 5 # Choose which line of the second_half.py should define the variable
with open('second_half.py','r') as f:
    file = f.readlines()

if a == 'y':
    file.insert(line,'\na = "y"')
    with open('second_half.py','w') as f:
        f.write(''.join(file))
else:
    file.insert(line,'\na = "n"')
    with open('second_half.py','w') as f:
        f.write(''.join(file))

import os
os.system('second_half.py')

This might not be what you're looking for, but does work for some specific situations.

Red
  • 26,798
  • 7
  • 36
  • 58
  • Unfortunately, this won't work for me. I have a while loop inside which I'm checking for some requirements, which when not fulfilled, the user will get a prompt asking if they want to continue. Thanks for the efforts tho – aditya May 29 '20 at 19:11
  • I'm sorry, I'll see if there's another way. – Red May 29 '20 at 19:23
0

You can do that with subprocess.run().

scenox
  • 698
  • 7
  • 17