0

I'm writing a python script in which I want to

  1. Create and subsequently open a text file using the user's system's default text editor.
  2. Wait for the user to add text to that file, save the file, and close the text editor.
  3. Read from the file that the user has saved.

I have already completed Step 1, but I don't know how to begin with Step 2. Broadly speaking, how would I go about pausing execution of the script while the user adds input to the text file and then restart execution upon exit from the text editor?

3
  • 2
    Take a look at the subprocess module -- it sounds like you want to open $EDITOR in a subprocess and then just read the file normally after it completes. Commented Mar 19, 2015 at 19:29
  • 2
    I think th OP is looking for a far more simpler solution. You could use an unassigned input('Press enter after you have made changes to the file) which is a crude way of "pausing" your program so that the user makes his/her changes Commented Mar 19, 2015 at 19:32
  • I'm currently using os.startfile to open the file. Do you know if there's a way to do this without changing my previously written code? Commented Mar 19, 2015 at 19:33

1 Answer 1

5

You can use subprocess module and wait for process to finish.

import subprocess
p = subprocess.Popen(('gedit',"testFile"))
p.wait()
print file("testFile").readlines()
Sign up to request clarification or add additional context in comments.

6 Comments

Do you know how I would get the system's default text editor to use as the first argument to Popen?
You can use environment variable EDITOR to access default editor in unix like systems.
subprocess.Popen(["start", "/WAIT", "file.txt"], shell=True) ought to do that @Danny
@Heisenberg Is there such a variable for windows?
@wnnmaw I will try this. Is there a way to accomplish the same thing without using shell=True? According to the python documentation, it can lead to security vulnerabilities.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.