How to Increment a Filename in Python?

Challenge: Given a Python program that writes data into a file. If you run the program again, it’ll overwrite the file written by the first execution of the program. Each time you run this program, the original content in file.dat will be overwritten. How to avoid this overwriting by adding an integer suffix to the … Read more

How to Check Whether a File Exists Without Exceptions?

Challenge: Given a string ‘/path/to/file.py’. How to check whether a file exists at ‘/path/to/file.py’, without using the try and except statements for exception handling? # What You Want! if exists(‘/path/to/file.py’): … # Do something Solution: To check whether a file exists at a given path, Run from pathlib import Path to import the path object, … Read more

Python map() — Finally Mastering the Python Map Function [+Video]

The map() function transforms one or more iterables into a new one by applying a “transformator function” to the i-th elements of each iterable. The arguments are the transformator function object and one or more iterables. If you pass n iterables as arguments, the transformator function must be an n-ary function taking n input arguments. … Read more

Python help()

Like most coders, I regularly consult a search engine—yeah, as if there were many good options ;)—to learn about parameter lists of specific Python functions. If you truly stand on the shoulders of giants, and leverage the powerful Python libraries developed by some of the best coders in the world, studying the API of existing … Read more