1

I'm trying to create a very simple text editor for an MS-DOS style program I'm making using python. My problem comes when I try to create paragraphs. The way I made it, pressing Enter tells it to save the input. I understand what I did wrong, but how can I fix it and how could I break the input? So far I have this:

def textwriter():
    print("")
    print("Start typing to begin.")
    textwriterCommand = input(" ")
    saveAs = input("Save file as: ")
    with open(saveAs, 'w') as f:
        f.write(textwriterCommand)
J. Bridges
  • 149
  • 1
  • 7
  • 1
    This might be what you want but I'm not sure how you break the input then. http://stackoverflow.com/a/3289051/5889975 – steven Feb 19 '16 at 23:28
  • If you use the [answer](http://stackoverflow.com/questions/1450393/how-do-you-read-from-stdin-in-python/3289051#3289051) that @steven linked to, you can watch for two blank lines (lines consisting of a newline only) in a row — perhaps by setting a flag — to break out of the loop. This is commonly how I've seen it handled. – martineau Feb 20 '16 at 01:06
  • I've updated my answer so the examples actually work in the way one would expect, which should be more helpful -- specifically, the second example works perfectly, with newlines – cat Feb 20 '16 at 05:05
  • This isn't well specified. What is the **rule that tells you** when the input is done? – Karl Knechtel Feb 06 '23 at 10:23

2 Answers2

2

Assign some other EOF sequence, for instance:

EOF_SEQ = 'EOF'

def textwriter():
    print("")
    print("Start typing to begin.")
    buffer = ''
    while EOF_SEQ not in buffer:
        buffer += raw_input(" ") + '\n'
    saveAs = raw_input("Save file as: ")
    with open(saveAs, 'w') as f:
        f.write(buffer)
leongold
  • 1,004
  • 7
  • 14
0

@zamuz's answer is good, and good for simple things like this, but when your code gets more complex, I prefer to use something like input_constrain, which actually tests each keypress to decide what to do next. (Full disclosure: I wrote this.)

For example, to read input until the user presses | (vertical bar):

from input_constrain import until

def textwriter():
    print("")
    print("Start typing to begin.")
    textwriterCommand = until("|")
    saveAs = input("Save file as: ")
    with open(saveAs, 'w') as f:
        f.write(textwriterCommand)

Or, to read input until CTRL - C or CTRL - D (or any other unprintable control char):

from input_constrain import until_not
from string import printable as ALLOWED_CHARS

def textwriter():
    print("")
    print("Start typing to begin.")
    textwriterCommand = until_not(ALLOWED_CHARS, count=5000, raw=True)
    saveAs = input("Save file as: ")
    with open(saveAs, 'w') as f:
        f.write(textwriterCommand)

This uses an optional count parameter, which will stop reading after exactly this many keypresses. raw=True is important because otherwise, CTRL - C and CTRL - D will throw exceptions. By using raw, we can break and continue the task at hand.


Important: You need commit 06e4bf3e72e23c9700734769e42d2b7fe029a2c1 because it contains fixes but no breaking changes

Community
  • 1
  • 1
cat
  • 3,888
  • 5
  • 32
  • 61