2

I have a script to download some files with urllib.request.urlretrieve and I use tqdm for my progress bar and that work fine, except when I use in the IDLE because there the others control characters like '\r' or '\b' don't work properly and as result the output is ugly

that is

IDLE

>>> print('abc\rXXX')
abc\rXXX
>>> print('abc\b\b\bXXX')
abc\b\b\bXXX
>>>

terminal/cmd

>>> print('abc\rXXX')
XXX
>>> print('abc\b\b\bXXX')
XXX
>>>

my question is, how I can detect when '\r' will work properly??

so I can do something like this

from tqdm import tqdm, tqdm_gui
if control_char_work_fine():
    progress_bar = tqdm
else:
    progress_bar = tqdm_qui

or if that is no possible how to detect when I am in the IDLE?

timrau
  • 22,578
  • 4
  • 51
  • 64
Copperfield
  • 8,131
  • 3
  • 23
  • 29
  • The [`curses` library](https://docs.python.org/2/library/curses.html) lets you query various things about a terminal, but you'd have to call `curses.initscr()` first, which is rather.. invasive in a terminal. – Martijn Pieters Mar 26 '16 at 16:33
  • Don't have `IDLE`, but what does `import os ; os.environ['TERM']` print in that terminal? – Joachim Isaksson Mar 26 '16 at 16:35
  • 2
    @JoachimIsaksson on windows it give a `KeyError`. – John Mar 26 '16 at 16:36
  • @JoachimIsaksson in the IDLE and in the `cmd` I get a `keyError`, in terminal I get `'xterm'` – Copperfield Mar 26 '16 at 16:42
  • 3
    I think I've found it, http://stackoverflow.com/a/17135271/322909 – John Mar 26 '16 at 16:49
  • @MartijnPieters in windows I get a `ImportError: No module named '_curses' in ubuntu using the IDLE I get a error `_curses.error: setupterm: could not find terminal` and in the terminal, after I call that I can not do anithing else, so I closed... – Copperfield Mar 26 '16 at 16:51
  • @John nice, that work in python 2,3 windows and ubuntu – Copperfield Mar 26 '16 at 17:02
  • 1
    Previous discussion of the \r issue is at https://stackoverflow.com/questions/35895864/what-is-the-difference-between-cmd-and-idle-when-using-tqdm. I think `'idlelib' in sys.modules` is the best test for 'running under IDLE'. – Terry Jan Reedy Mar 26 '16 at 18:24
  • @TerryJanReedy yep, that was my previous question on the subject – Copperfield Mar 26 '16 at 18:33

0 Answers0