Neculai Fântânaru

Everything Depends On The Leader

Python: Inverts the letters in the words as if the words were seen in the mirror

On May 05, 2021
, in
Python Scripts Examples by Neculai Fantanaru

You can view the full code here: https://pastebin.com/3pnjgwNC

import re

def read_text_from_file(file_path):
    """
    This function returns the contents of a file.
    """
    with open(file_path, 'r') as f:
        text = f.read()
        return text

def write_to_file(text, file_path):
    """
    This function writes text to a file.
    text: the text you want to write
    """
    with open(file_path, 'w') as f:
        f.write(text)

def reverse_words(file_path):
    """
    This function inverts all words in a file.
    """
    
    text = read_text_from_file(file_path)
    words = set(re.findall(r'[A-Za-z0-9\-\']+', text))
    for word in words:
        reversed_word = ''.join(reversed(word))
        text = text.replace(word, reversed_word)
    # we write the modified text in the file !!! attention, the content is overwritten !!!
    write_to_file(text, file_path)

if __name__ == '__main__':
	# the path of your file
    file_path = 'd:\Exercises\1'   
    reverse_words(file_path)

Other examples:

https://superuser.com/questions/1684040/

https://community.notepad-plus-plus.org/topic/22054/

That's all folks.

If you like my code, then make me a favor: translate your website into Romanian, "ro".

Also, see this VERSION 2 or VERSION 3 or VERSION 4 or VERSION 5 or VERSION 6 or VERSION 7

Alatura-te Comunitatii Neculai Fantanaru
The 63 Greatest Qualities of a Leader
Cele 63 de calităţi ale liderului

Why read this book? Because it is critical to optimizing your performance. Because it reveals the main coordinates after that are build the character and skills of the leaders, highlighting what it is important for them to increase their influence.

Leadership - Magic of Mastery
Atingerea maestrului

The essential characteristic of this book in comparison with others on the market in the same domain is that it describes through examples the ideal competences of a leader. I never claimed that it's easy to become a good leader, but if people will...

The Master Touch
Leadership - Magia măiestriei

For some leaders, "leading" resembles more to a chess game, a game of cleverness and perspicacity; for others it means a game of chance, a game they think they can win every time risking and betting everything on a single card.

Leadership Puzzle
Leadership Puzzle

I wrote this book that conjoins in a simple way personal development with leadership, just like a puzzle, where you have to match all the given pieces in order to recompose the general image.

Performance in Leading
Leadership - Pe înţelesul tuturor

The aim of this book is to offer you information through concrete examples and to show you how to obtain the capacity to make others see things from the same angle as you.

Leadership for Dummies
Leadership - Pe înţelesul tuturor

Without considering it a concord, the book is representing the try of an ordinary man - the author - who through simple words, facts and usual examples instills to the ordinary man courage and optimism in his own quest to be his own master and who knows... maybe even a leader.