6 Best Python AI scripts for everyday use

6 Python AI script for everyday use
Python AI script

Introduction

Python AI scripts can be used for a variety of everyday tasks. If you’ve ever wanted to automate tedious tasks to make your life easier, then this is the post for you. We will cover five simple yet powerful Python AI (Artificial Intelligence) scripts that can help you unlock automation in your everyday life. Throughout this post, you’ll learn how to use Python scripting to rename files in bulk, collect data from different sources, scrape websites for data, generate meaningful reports and even automate testing processes. By the end of this post, you’ll have a newfound appreciation and knowledge of how Python scripting can help simplify and save time on mundane tasks. So let’s get started!

1 Download webpages from a list of URLs

Python offers a convenient way to download webpages from a list of URLs. It can be done with the help of the ‘requests’ library, which allows us to easily make requests for each URL in the list and store the response. Once all responses are obtained, we can write them into a file or use any other method for further processing. This is highly useful when performing batch operations on large datasets that involve downloading many webpages.

CODE

#import Libraries 
import requests 
import urllib.request 

# URLs list 
urls = ['https://www.example1.com', 'https://www.example2.com', 
        'https://www.example3.com'] 

# Iterate over urls list 
for url in urls: 
	# Get the webpage 
	r = requests.get(url) 

	# Extract the filename from the url 
	# split function returns a list containing the 
	# words present in the url 
	filename = url.split('/')[-1] 

	# Open a local file with wb ( write binary ) permission 
	with open(filename, 'wb') as f: 

		# Write the content of the response ( r.content ) to a local file 
		f.write(r.content) 

# Function to download file 
def download(url, filename): 
	
	""" 
	Function to download file 
	url: URL of the file to be downloaded 
	filename: Name of the local file 
	"""

	# Open the url 
	r = requests.get(url) 

	# Open a local file with wb ( write binary ) permission 
	with open(filename, 'wb') as f: 

		# Write the content of the response ( r.content ) 
		# to a local file 
		f.write(r.content)

2 Create a text file from a list of strings using Python AI Script

Creating a text file from a list of strings in Python is simple and straightforward. By using the ‘open’ method, you can create a new text file with the name of your choice. Then, loop through each item on the given list and write them to the opened text file. To ensure proper formatting, you must add a new line character after each item is written to the file. Finally, remember to close the file when done writing to it. This process will allow you to save an entire list of strings as one text file for future use

CODE

strings = ['This is the first line of text',
           'This is the second line of text',
           'This is the third line of text']
           
with open('example.txt', 'w') as file:
    for line in strings:
        file.write(line + '\n')

3 Generate reports from data sets

Python is a powerful language used for data analysis, as it allows users to generate reports from datasets quickly and easily. It also provides extensive libraries which enable users to access all sorts of data structures, such as numerical tables, csv files or plain text files. Additionally, Python can leverage its matplotlib library for graphical representation of the datasets into histograms, scatter plots, pie charts and many more. Finally, Python’s pandas library enables users to clean up huge datasets by way of removing outliers and organizing the data into an effective format for better understanding. All in all, utilizing Python’s report generating capabilities makes understanding large sets of data easier while providing greater accuracy with fewer errors.

CODE

import pandas as pd

file = 'dataset.csv'
df = pd.read_csv(file)

writer = pd.ExcelWriter('report.xlsx')
df.to_excel(writer)
writer.save()

4 Automate file renaming and sorting

Python can be used to automate the process of renaming and sorting files in a directory. This process involves looping through the directory, reading in each file name, parsing the file name into components, constructing a new file name based on a predefined naming convention, and writing out the renamed files. By automating this process using Python, users are able to ensure consistent naming conventions across multiple directories or across multiple data sets. In addition, automated sorting by date, type or size can group related files together for easy inspection and categorization. Automating these processes saves user time and effort while providing consistency in data organization.

CODE

import os

dir = 'user/documents/'
files = os.listdir(dir)

for old_file_name in files:
    new_file_name = old_file_name.replace(' ', '_')
    os.rename(dir + old_file_name, dir + new_file_name)

5 Create an automated reminders system

Python scripts can be used to create an automated reminders system. The script will allow users to set specific dates and times for reminders, as well as any related notes or comments. Once the reminder is set, the script will automatically send out alerts via email, text message, or other notifications. This allows users to have automated control over their reminders so they don’t miss important meetings, deadlines, or other tasks. The script can also be customized according to user needs such as language type, display format and more. Automated reminders systems created with python scripts are beneficial for keeping track of due dates and appointments while reducing user workload and ensuring that no important tasks fall through the cracks.

CODE

import datetime
import smtplib

people = {'name1': 'email1@example.com', 
          'name2': 'email2@example.com'
          }
          
tasks = {'task1': datetime.date(year, month, day),
         'task2': datetime.date(year, month, day)
         }
         
for name, email in people.items():
   for task, due_date in tasks.items():
       if datetime.date.today() == due_date - datetime.timedelta(days=1):
          message = 'Hi ' + name + '! This is a reminder that ' + task + 
          ' is due tomorrow.'
          server = smtplib.SMTP('smtp.example.com', 587)
          server.starttls()
          server.login('username', 'password')
          break
      server.sendmail(email,message)
      server.quit()

6 Generate random passwords from user-defined criteria

Python script can be used to generate random passwords based on user-defined criteria. This script allows users to set a minimum and maximum length for the password, as well as set criteria for symbols, alphanumerics and special characters. The generated passwords are randomly generated and comply with the user-defined criteria, making them more secure and harder to guess. Furthermore, they are stored in a secure location so that their identities can remain anonymous.

CODE

import string
import random

def generate_password():
    length = 10
    chars = string.ascii_uppercase + string.ascii_lowercase + string.digits
    password = ''.join(random.choice(chars) for _ in range(length))
    return password

print(generate_password())

Conclusion

To conclude, Python programming is an invaluable scripting language to have in your programming toolkit. With its easy-to-learn syntax and flexible libraries, you can create powerful scripts that will automate everyday tasks quickly and easily. By learning the five scripts we discussed here, you are already on your way to unlocking the potential of automation. You can then continue to build upon these scripts and take your automation skills even further.

admin
admin

Leave a Reply

Your email address will not be published. Required fields are marked *