5 Awsome Python Scripts
Here are some awesome Python scripts that you might find interesting and useful for various tasks:
File Organizer: This script can organize files in a directory based on their extensions. It can create folders for different file types and move files into their respective folders.
pythonCopy codeimport os import shutil def organize_files(directory): for filename in os.listdir(directory): if os.path.isfile(os.path.join(directory, filename)): file_extension = filename.split(".")[-1] if not os.path.exists(file_extension): os.makedirs(file_extension) shutil.move(os.path.join(directory, filename), os.path.join(file_extension, filename)) if __name__ == "__main__": target_directory = "your_target_directory_path_here" organize_files(target_directory)
Web Scraper: This script can scrape data from a website using libraries like BeautifulSoup and requests. You can adapt it to extract specific information from web pages.
pythonCopy codeimport requests from bs4 import BeautifulSoup def scrape_website(url): response = requests.get(url) if response.status_code == 200: soup = BeautifulSoup(response.text, 'html.parser') # Add your scraping logic here else: print("Failed to retrieve the web page.") if __name__ == "__main__": target_url = "https://example.com" scrape_website(target_url)
Password Generator: This script generates strong random passwords.
pythonCopy codeimport random import string def generate_password(length=12): characters = string.ascii_letters + string.digits + string.punctuation password = ''.join(random.choice(characters) for _ in range(length)) return password if __name__ == "__main__": password = generate_password() print("Generated Password:", password)
Image Downloader: Downloads images from a list of URLs.
pythonCopy codeimport os import requests def download_images(url_list, download_folder): if not os.path.exists(download_folder): os.makedirs(download_folder) for i, url in enumerate(url_list): response = requests.get(url) if response.status_code == 200: with open(os.path.join(download_folder, f"image_{i+1}.jpg"), 'wb') as f: f.write(response.content) else: print(f"Failed to download image {i+1} from {url}") if __name__ == "__main__": urls = ["https://example.com/image1.jpg", "https://example.com/image2.jpg"] download_folder = "your_download_directory_path_here" download_images(urls, download_folder)
PDF Text Extractor: Extracts text content from PDF files using the PyPDF2 library.
pythonCopy codeimport PyPDF2 def extract_text_from_pdf(pdf_path): text = "" with open(pdf_path, 'rb') as pdf_file: pdf_reader = PyPDF2.PdfFileReader(pdf_file) for page_num in range(pdf_reader.numPages): page = pdf_reader.getPage(page_num) text += page.extractText() return text if __name__ == "__main__": pdf_file_path = "your_pdf_file_path_here.pdf" extracted_text = extract_text_from_pdf(pdf_file_path) print("Extracted Text:") print(extracted_text)
These scripts cover a range of common tasks and can serve as a starting point for your Python projects. Remember to adapt and modify them according to your specific needs.