1

I'm using IDLE (Python 2.7) on Debian Wheezy. I sourced questions here and here. I tried applying what they said, and I got some strange results. If I print carriage returns or backspaces in the Python shell, rather than actually getting those, I get squares with bs and cr in them. I can't show them here because they don't copy over.

How can I get an actual carriage return? This is my code, based on the questions I cited above:

sys.stdout.write("Extracted: %d% Skipped %d% \r" % (len(f),len(skipped)))
sys.stdout.flush()
Community
  • 1
  • 1
Athena
  • 3,200
  • 3
  • 27
  • 35
  • 1
    It sounds like you did get one, but your terminal didn't do what you wanted. This is a terminal behavior issue. – FatalError Jun 29 '16 at 15:58
  • @FatalError the terminal in this case is IDLE, which is common enough to make this an answerable question. That said, I can't reproduce it on Windows with either Python 2.7 or 3.5. The OS and Python version should be added to the question. – Mark Ransom Jun 29 '16 at 16:04
  • Corrected it @MarkRansom. – Athena Jun 29 '16 at 16:05
  • IDLE on Ubuntu here also does not produce the usual carriage return behavior. I expect that if you run your code in a more general linux terminal you will see the behavior your are expecting. – FatalError Jun 29 '16 at 16:10
  • 1
    Except for `\n`, IDLE prints output in response to input as received. 1. IDLE is a development environment, not a runtime environment. Developers mostly need to see literal output. To see runtime behavior, test in intended runtime environment. 2. Shell is a text window, not a terminal. 3. The text window is based on tk Text, so that determines default behavior. 4. There is no standard terminal. The unix termcap system is evidence. 5. Should backspace/return delete as well as move? 6. For complete control of output use tkinter. 7. There should be terminal widgets available on PyPI. – Terry Jan Reedy Jun 29 '16 at 19:09
  • 1
    In spite of what I said above, I have considered adding simple backspace/return interpretation to IDLE *as an option*. But see 5. above. There is also an issue with how to turn on the option from within a program, or at least the command line. – Terry Jan Reedy Jun 29 '16 at 19:12

1 Answers1

1

Terminals vary in their capabilites of handling such special characters. A similar question was answered by Wayne Werner, who said that IDLE just does not handle those special characters (i.e. printing a stand-in character, rather than moving the cursor or deleting.), which may be for good reason.

Short of writing a patch as Wayne mentioned, you may find that running your program in the native terminal does work as expected. (i.e. python path/to/your/program).

From Comments

If you would like the program to be aware of it's environment (as was asked in the comments) it is possible (and perhaps most simple) to create a command line switch that is set manually at the program's execution. If you launch from a shortcut or another program (perhaps a batch file) you could automate the process by incorporating it into the pre-written command.

Such a switch could look like:

python path/to/your/program --terminal //For a full terminal
python path/to/your/program --IDLE //For IDLE

Note

After reading Terry Jan Reedy's comment, I'd to point out that his guidance toward tkinter(and on the general facets of this situation) may in fact be the most helpful for a thorough solution to the issue. Check those comments out.

Community
  • 1
  • 1
rtmh
  • 439
  • 3
  • 9
  • How can I tell which one I'm in, though? If that's the case, I only really want to show this behavior in terminals that are capable of displaying it. – Athena Jun 29 '16 at 16:11
  • 1
    @ares Interesting idea, my quick search didn't find much of anything promising. [This question](http://stackoverflow.com/q/9839240/2465194) has a number of somewhat hackish solutions that you may want to try. – rtmh Jun 29 '16 at 16:16
  • 1
    @ares, perhaps the most straightforward approach is introducing a command line switch that you set manually. If you launch from a shortcut or another program you could hide this switch by incorporating into the command. – rtmh Jun 29 '16 at 16:17