How do I print colored output to the terminal in Python?

Would the Python termcolor module do? This would be a rough equivalent for some uses. The example is right from this post, which has a lot more. Here is a part of the example from docs A specific requirement was to set the color, and presumably other terminal attributes, so that all following prints are that way. While I … Read more

Asyncio.gather vs asyncio.wait

Although similar in general cases (“run and get results for many tasks”), each function has some specific functionality for other cases: asyncio.gather() Returns a Future instance, allowing high level grouping of tasks: All tasks in a group can be cancelled by calling group2.cancel() or even all_groups.cancel(). See also .gather(…, return_exceptions=True), asyncio.wait() Supports waiting to be stopped after the first … Read more

What is the difference between ‘/’ and ‘//’ when used for division?

In Python 3.x, 5 / 2 will return 2.5 and 5 // 2 will return 2. The former is floating point division, and the latter is floor division, sometimes also called integer division. In Python 2.2 or later in the 2.x line, there is no difference for integers unless you perform a from __future__ import division, which causes Python 2.x to adopt the 3.x behavior. … Read more

Create empty file using python [duplicate]

There is no way to create a file without opening it There is os.mknod(“newfile.txt”) (but it requires root privileges on OSX). The system call to create a file is actually open() with the O_CREAT flag. So no matter how, you’ll always open the file. So the easiest way to simply create a file without truncating it in case it exists is this: … Read more