Python: Send Message to Telegram

To send a message to a Telegram channel using Python, first of all it is required to create a Telegram bot.

While creating the Telegram bot you will get an apiToken and a chatID that will be used in a Python script to access the Telegram API and send the message.

In this note you will find the examples of the Python scripts for sending messages, images, etc. to Telegram through the API. (more…)

Python: Write to File | Read from File – Best Practice

To write something to a file or to read the content of a file in Python it is required to open() and close() the file properly.

The best practice in Python is to read/write to a file by using the with statement – the replacement for commonly used try...finally error-handling statements.

The with statement ensures that the file is closed when the block inside it is exited.

This note shows the best practice Python examples of how to write to a file or read the file’s content and save it to a variable. (more…)

Run Selenium Headless – Chrome | Firefox (Python)

Selenium is an open source tool for web browsers automation that is widely used in tandem with Python to test web applications.

As these tests are often automated and run on dedicated nodes, there is a sense to run Selenium in a headless mode, i.e. without opening a browser.

This note shows how to run headless Selenium in Python on the example of Chrome and Firefox browsers controlled by a ChromeDriver and GeckoDriver correspondingly. (more…)

Python Script Not Showing Output [SOLVED]

It may happen that you execute a Python script and nothing happenes, i.e. it seems that it “hangs” without showing any output.

Even if you explicitly include some print() statements to debug the issue, it may still not print anything to a terminal.

One of the reasons of why your Python script does not show any output is because it buffers stdout and stderr steams instead of printing them.

This also can happen if you execute the Python script from a CI/CD pipeline (e.g. using Jenkins, Gitlab-CI, TeamCity, etc.) or if you run it using a Dockerfile.

This short note shows how to solve the issue when the Python script “hangs” and doesn’t show any output. (more…)

Ansible: Skip Parameter If Variable Not Defined

In Ansible, if you run a task that has some module’s parameter with a variable that is not set you will get “The task includes an option with an undefined variable” error.

If for some reason you can’t or don’t want to define this variable, you can make it optional.

In this case Ansible will ignore the parameter with the optional variable if it is not defined.

This note shows how to skip a module’s parameter in Ansible if a variable is not defined. (more…)

Rename User Folder in Windows

User profiles in Windows on the most of computers are usually located in the folder C:\users\.

By default, the user folder name is the same as you user name.

But if you change your user name, the old name of your user folder will remain and you may want to rename it as well.

The renaming of the user folder in Windows is not so straightforward, but i will show how to do this in a quick, safe and easy way. (more…)