Collection of the 20 useful Python Scripts
Python is one of the easiest programming languages to learn. Furthermore, it is in high demand in the software development sphere. It allows you to automate processes, making tedious tasks, a walk in the park. In this article, we look at the 20 best Python scripts you can use today.
Let’ dive into it!
1. Converting hours to seconds
When working on projects that require you to convert hours into seconds, you can use the following Python script.
def convert(seconds):
seconds = seconds % (24 * 3600)
hour = seconds // 3600
seconds %= 3600
minutes = seconds // 60
seconds %= 60
return "%d:%02d:%02d" % (hour, minutes, seconds)
# Driver program
n = 12345
print(convert(n))
2. Raising a number to the power
Another popular Python script calculates the power of a number. For example, 2 to the power of 4. Here, there are at least three methods to choose from. You can use the math.pow(), pow(), or **. Here is the script.
import math
# Assign values to x and n
x = 4
n = 3
# Method 1
power = x ** n
print("%d to the power %d is %d" % (x,n,power))
# Method 2
power = pow(x,n)
print("%d to the power %d is %d" % (x,n,power))
# Method 3
power = math.pow(2,6.5)
print("%d to the power %d is %5.2f" % (x,n,power))
3. If/else statement
This is arguably one of the most used statements in Python. It allows your code to execute a function if a certain condition is met. Unlike other languages, you don’t need to use curly braces. Here is a simple if/else script.
# Assign a value
number = 50
# Check the is more than 50 or not
if (number >= 50):
print("You have passed")
else:
print("You have not passed")
4. Convert images to JPEG
The most conventional systems rarely accept image formats such as PNG. As such, you’ll be required to convert them into JPEG files. Luckily, there’s a Python script that allows you to automate this process.
import os
import sys
from PIL import Image
if len(sys.argv) > 1:
if os.path.exists(sys.argv[1]):
im = Image.open(sys.argv[1])
target_name = sys.argv[1] + ".jpg"
rgb_im = im.convert('RGB')
rgb_im.save(target_name)
print("Saved as " + target_name)
else:
print(sys.argv[1] + " not found")
else:
print("Usage: convert2jpg.py <file>")
5. Download Google images
If you are working on a project that demands many images, there’s a Python script that enables you to do so. With it, you can download hundreds of images simultaneously. However, you should avoid violating copyright terms. Click here for more information.
6. Read battery level of Bluetooth device
This script allows you to read the battery level of your Bluetooth headset. This is especially crucial if the level does not display on your PC. However, it does not support all Bluetooth headsets. For it to run, you need to have Docker on your system. Click here for more information.
7. Delete Telegram messages
Let’s face it, messaging apps do chew up much of your device’s storage space. And Telegram is no different. Luckily, this script allows you to delete all supergroups messages. You need to enter the supergroup’s information for the script to run. Click here for more information.
8. Get song lyrics
This is yet another popular Python script that enables you to scrape lyrics from the Genius site. It primarily works with Spotify, however, other media players with DBus MediaPlayer2 can also use the script. With it, you can sing along to your favorite song. Click here for more information.
9. Heroku hosting
Heroku is one of the most preferred hosting services. Used by thousands of developers, it allows you to build apps for free. Likewise, you can host your Python applications and scripts on Heroku with this script. Click here for more information.
10. Github activity
If you contribute to open source projects, keeping a record of your contributions is recommended. Not only do you track your contributions, but also appear professional when displaying your work to other people. With this script, you can generate a robust activity graph. Click here for information.
11. Removing duplicate code
When creating large apps or working on projects, it is normal to have duplicates in your list. This not only makes coding strenuous, but also makes your code appear unprofessional. With this script, you can remove duplicates seamlessly.
12. Sending emails
Emails are crucial to any businesses’ communication avenues. With Python, you can enable sites and web apps to send them without hiccups. However, businesses do not want to send each email manually, instead, they prefer to automate the process. This script allows you to choose which emails to reply to.
13. Find specific files on your system
Often, you forget the names or location of files on your system. This is not only annoying but also consumes time navigating through different folders. While there are programs that help you search for files, you need one that can automate the process.
Luckily, this script enables you to choose which files and file types to search for. For example, if want to search for MP3 files, you can use this script.
import fnmatch
import os
rootPath = '/'
pattern = '*.mp3'
for root, dirs, files in os.walk(rootPath):
for filename in fnmatch.filter(files, pattern):
print( os.path.join(root, filename))
14. Generating random passwords
Passwords bolster the privacy of app and website users. Besides, they prevent fraudulent use of accounts by cyber criminals. As such, you need to create an app or website that can generate random strong passwords. With this script, you can seamlessly generate them.
import string
from random import *
characters = string.ascii_letters + string.punctuation + string.digits
password = "".join(choice(characters) for x in range(randint(8, 16)))
print (password)
15. Print odd numbers
Some projects may require you to print odd numbers within a specific range. While you can do this manually, it is time-consuming and prone to error. This means you need a program that can automate the process. Thanks to this script, you can achieve this.
16. Get date value
Python allows you to format a date value in numerous ways. With the DateTime module, this script allows you to read the current date and set a custom value.
17. Removing items from a list
You’ll often have to modify lists on your projects. Python enables you to do this using the Insert() and remove() methods. Here is a script you can use to achieve this.
# Declare a fruit list
fruits = ["Mango","Orange","Guava","Banana"]
# Insert an item in the 2nd position
fruits.insert(1, "Grape")
# Displaying list after inserting
print("The fruit list after insert:")
print(fruits)
# Remove an item
fruits.remove("Guava")
# Print the list after delete
print("The fruit list after delete:")
print(fruits)
18. Count list items
Using the count() method, you can print how many times a string appears in another string. You need to provide the string that Python will search. Here is a script to help you do so.
# Define the string
string = 'Python Bash Java PHP PHP PERL'
# Define the search string
search = 'P'
# Store the count value
count = string.count(search)
# Print the formatted output
print("%s appears %d times" % (search, count))
19. Text grabber
With this Python script, you can take a screenshot and copy the text in it. Click here for more information.
20. Tweet search
Ever searched for a tweet to no avail? Annoying, right! Well, why not use this script and let it do the legwork for you.
Final verdict
You see, Python allows us to automate some of the most mundane processes. And with these best python scripts, you can make your app development process stress-free. So, which Python scripts do you use? Let us know in the comment section.