Task:1
Consider you have many photos in a folder. Check their Properties. In Properties, you have created date. Move all photos which have specific Created Date to a different folder.
import osimport shutilimport datetimesource_folder = r"/home/guru/Desktop/Guru/Python/images"destination_folder = r"/home/guru/Desktop/Guru/Python/images2"target_date = "2025-01-11"os.makedirs(destination_folder, exist_ok=True)for file_name in os.listdir(source_folder):file_path = os.path.join(source_folder, file_name)if os.path.isfile(file_path):created_time = os.stat(file_path).st_mtimecreated_date = datetime.datetime.fromtimestamp(created_time).strftime("%Y-%m-%d")if created_date == target_date:shutil.move(file_path, os.path.join(destination_folder, file_name))print(f"Moved: {file_name}")print("Task completed.")import os import shutil import datetime source_folder = r"/home/guru/Desktop/Guru/Python/images" destination_folder = r"/home/guru/Desktop/Guru/Python/images2" target_date = "2025-01-11" os.makedirs(destination_folder, exist_ok=True) for file_name in os.listdir(source_folder): file_path = os.path.join(source_folder, file_name) if os.path.isfile(file_path): created_time = os.stat(file_path).st_mtime created_date = datetime.datetime.fromtimestamp(created_time).strftime("%Y-%m-%d") if created_date == target_date: shutil.move(file_path, os.path.join(destination_folder, file_name)) print(f"Moved: {file_name}") print("Task completed.")import os import shutil import datetime source_folder = r"/home/guru/Desktop/Guru/Python/images" destination_folder = r"/home/guru/Desktop/Guru/Python/images2" target_date = "2025-01-11" os.makedirs(destination_folder, exist_ok=True) for file_name in os.listdir(source_folder): file_path = os.path.join(source_folder, file_name) if os.path.isfile(file_path): created_time = os.stat(file_path).st_mtime created_date = datetime.datetime.fromtimestamp(created_time).strftime("%Y-%m-%d") if created_date == target_date: shutil.move(file_path, os.path.join(destination_folder, file_name)) print(f"Moved: {file_name}") print("Task completed.")
Enter fullscreen mode Exit fullscreen mode
Summary:
1) Imports Required Modules → Uses os for file handling, shutil for moving files, and datetime for date formatting.
2) Defines Source and Destination Folders → Specifies where the images are stored and where they should be moved.
3) Sets Target Date → Filters files based on their last modified date (formatted as YYYY-MM-DD).
4) Creates Destination Folder If It Doesn’t Exist → Ensures the folder is available before moving files.
5) Loops Through Files in the Source Folder → Checks each file one by one.
6) Extracts the Last Modified Date → Converts the file’s last modified timestamp into a human-readable format.
7) Compares the File’s Date with the Target Date → If they match, the file is selected for moving.
8) Moves the File to the Destination Folder → Transfers matching files to the specified location.
9) Prints Status Messages → Displays which files were moved and confirms completion.
Task:2
Write a program to open google.com and search for any keyword. Try to get all the links given by Google. [Use Selenium Module to complete this]
from selenium import webdriverfrom selenium.webdriver.chrome.service import Servicefrom selenium.webdriver.common.by import Byfrom selenium.webdriver.common.keys import Keysimport timedriver_path = '/usr/local/bin/chromedriver'service = Service(driver_path)driver = webdriver.Chrome(service=service)driver.get("http://google.com")time.sleep(2)search_box = driver.find_element(By.NAME, "q")search_box.send_keys("python interview questions")search_box.send_keys(Keys.RETURN)time.sleep(5)search_results = driver.find_elements(By.CSS_SELECTOR, "h3")while not search_results:time.sleep(2)search_results = driver.find_elements(By.CSS_SELECTOR, "h3")for result in search_results:try:parent = result.find_element(By.XPATH, "./ancestor::a")link = parent.get_attribute("href")if link:print(link)except Exception as e:print(f"Skipping result due to error: {e}")continuedriver.quit()from selenium import webdriver from selenium.webdriver.chrome.service import Service from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys import time driver_path = '/usr/local/bin/chromedriver' service = Service(driver_path) driver = webdriver.Chrome(service=service) driver.get("http://google.com") time.sleep(2) search_box = driver.find_element(By.NAME, "q") search_box.send_keys("python interview questions") search_box.send_keys(Keys.RETURN) time.sleep(5) search_results = driver.find_elements(By.CSS_SELECTOR, "h3") while not search_results: time.sleep(2) search_results = driver.find_elements(By.CSS_SELECTOR, "h3") for result in search_results: try: parent = result.find_element(By.XPATH, "./ancestor::a") link = parent.get_attribute("href") if link: print(link) except Exception as e: print(f"Skipping result due to error: {e}") continue driver.quit()from selenium import webdriver from selenium.webdriver.chrome.service import Service from selenium.webdriver.common.by import By from selenium.webdriver.common.keys import Keys import time driver_path = '/usr/local/bin/chromedriver' service = Service(driver_path) driver = webdriver.Chrome(service=service) driver.get("http://google.com") time.sleep(2) search_box = driver.find_element(By.NAME, "q") search_box.send_keys("python interview questions") search_box.send_keys(Keys.RETURN) time.sleep(5) search_results = driver.find_elements(By.CSS_SELECTOR, "h3") while not search_results: time.sleep(2) search_results = driver.find_elements(By.CSS_SELECTOR, "h3") for result in search_results: try: parent = result.find_element(By.XPATH, "./ancestor::a") link = parent.get_attribute("href") if link: print(link) except Exception as e: print(f"Skipping result due to error: {e}") continue driver.quit()
Enter fullscreen mode Exit fullscreen mode
Output:
https://www.geeksforgeeks.org/python-interview-questions/https://www.interviewbit.com/python-interview-questions/https://www.w3schools.com/python/python_interview_questions.asphttps://www.simplilearn.com/tutorials/python-tutorial/python-interview-questionshttps://www.youtube.com/watch?v=WH_ieAsb4AIhttps://www.ccbp.in/blog/articles/python-full-stack-developer-interview-questionshttps://www.javatpoint.com/python-interview-questionshttps://www.datacamp.com/blog/top-python-interview-questions-and-answershttps://www.geeksforgeeks.org/python-interview-questions/https://uk.indeed.com/career-advice/interviewing/python-interview-questionshttps://www.coursera.org/in/articles/what-is-python-used-for-a-beginners-guide-to-using-python#:~:text=Python%20is%20a%20computer%20programming,used%20to%20create%20various%20programmes.https://in.indeed.com/career-advice/finding-a-job/what-is-python-full-stack-developer#:~:text=Updated%2027%20June%202024,a%20website%20or%20an%20application.https://www.naukri.com/code360/library/python-interview-questionshttps://github.com/Tanu-N-Prabhu/Python/blob/master/Python%20Coding%20Interview%20Prep/Python%20Coding%20Interview%20Questions%20(Beginner%20to%20Advanced).mdhttps://www.geeksforgeeks.org/python-interview-questions/ https://www.interviewbit.com/python-interview-questions/ https://www.w3schools.com/python/python_interview_questions.asp https://www.simplilearn.com/tutorials/python-tutorial/python-interview-questions https://www.youtube.com/watch?v=WH_ieAsb4AI https://www.ccbp.in/blog/articles/python-full-stack-developer-interview-questions https://www.javatpoint.com/python-interview-questions https://www.datacamp.com/blog/top-python-interview-questions-and-answers https://www.geeksforgeeks.org/python-interview-questions/ https://uk.indeed.com/career-advice/interviewing/python-interview-questions https://www.coursera.org/in/articles/what-is-python-used-for-a-beginners-guide-to-using-python#:~:text=Python%20is%20a%20computer%20programming,used%20to%20create%20various%20programmes. https://in.indeed.com/career-advice/finding-a-job/what-is-python-full-stack-developer#:~:text=Updated%2027%20June%202024,a%20website%20or%20an%20application. https://www.naukri.com/code360/library/python-interview-questions https://github.com/Tanu-N-Prabhu/Python/blob/master/Python%20Coding%20Interview%20Prep/Python%20Coding%20Interview%20Questions%20(Beginner%20to%20Advanced).mdhttps://www.geeksforgeeks.org/python-interview-questions/ https://www.interviewbit.com/python-interview-questions/ https://www.w3schools.com/python/python_interview_questions.asp https://www.simplilearn.com/tutorials/python-tutorial/python-interview-questions https://www.youtube.com/watch?v=WH_ieAsb4AI https://www.ccbp.in/blog/articles/python-full-stack-developer-interview-questions https://www.javatpoint.com/python-interview-questions https://www.datacamp.com/blog/top-python-interview-questions-and-answers https://www.geeksforgeeks.org/python-interview-questions/ https://uk.indeed.com/career-advice/interviewing/python-interview-questions https://www.coursera.org/in/articles/what-is-python-used-for-a-beginners-guide-to-using-python#:~:text=Python%20is%20a%20computer%20programming,used%20to%20create%20various%20programmes. https://in.indeed.com/career-advice/finding-a-job/what-is-python-full-stack-developer#:~:text=Updated%2027%20June%202024,a%20website%20or%20an%20application. https://www.naukri.com/code360/library/python-interview-questions https://github.com/Tanu-N-Prabhu/Python/blob/master/Python%20Coding%20Interview%20Prep/Python%20Coding%20Interview%20Questions%20(Beginner%20to%20Advanced).md
Enter fullscreen mode Exit fullscreen mode
Explanation:
- Import required modules.
- Set Up WebDriver with its path.
- Open Google
- Locate Search Box and Enter keyword to be searched then submit using
RETURN
. - Wait for results to load for 5 seconds –
time.sleep(5)
- Find Search result titles containing
<h3>
tags. -
try-except
block is used to find and handle errors.If any error occurs it will print the error. - Extract Parent
<a>
Tag usingXPath
& Gethref
attribute for Links. - Then Close the Browser using
driver.quit()
.
原文链接:Python Weekend Tasks
暂无评论内容