25 个很棒的 Python 脚本合集(迷你项目) - PDF 下载,2024年最新高级Python面试题2024

先自我介绍一下,小编浙江大学毕业,去过华为、字节跳动等大厂,目前阿里P7

深知大多数程序员,想要提升技能,往往是自己摸索成长,但自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年最新Python全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友。
img
img



既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,涵盖了95%以上Python知识点,真正体系化!

由于文件比较多,这里只是将部分目录截图出来,全套包含大厂面经、学习笔记、源码讲义、实战项目、大纲路线、讲解视频,并且后续会持续更新

如果你需要这些资料,可以添加V获取:vip1024c (备注Python)
img

正文

import json``if name == ‘main’: try: with open(‘input.json’, ‘r’) as f: data = json.loads(f.read())`

output = ','.join([*data[0]]) for obj in data: output += f'\n{obj["Name"]},{obj["age"]},{obj["birthyear"]}'

with open('output.csv', 'w') as f: f.write(output) except Exception as ex: `print(f’Error: {str(ex)}')

2.密码生成器


import randomimport stringtotal = string.ascii_letters + string.digits + string.punctuationlength = 16password = “”.join(random.sample(total, length))``print(password)

3.从多个文件中搜索字符串


​​​​​​​

import ostext = input("input text : ")path = input("path : ")# os.chdir(path)def getfiles(path): f = 0 os.chdir(path) files = os.listdir() # print(files) for file_name in files: abs_path = os.path.abspath(file_name) if os.path.isdir(abs_path): getfiles(abs_path) if os.path.isfile(abs_path): f = open(file_name, “r”) if text in f.read(): f = 1 print(text + " found in ") final_path = os.path.abspath(file_name) print(final_path) return True if f == 1: print(text + " not found! ") return False`

`getfiles(path)

4.从给定网页获取所有链接


import requests as rq``from bs4 import BeautifulSoup`

url = input("Enter Link: ")``if ("https" or "http") in url: data = rq.get(url)``else: data = rq.get("https://" + url)``soup = BeautifulSoup(data.text, "html.parser")``links = []``for link in soup.find_all("a"): links.append(link.get("href"))

# Writing the output to a file (myLinks.txt) instead of to stdout``# You can change 'a' to 'w' to overwrite the file each time``with open("myLinks.txt", 'a') as saved: `print(links[:10], file=saved)

5.图像水印


​​​​​​​

import os``from PIL import Image`

def watermark_photo(input_image_path,watermark_image_path,output_image_path): base_image = Image.open(input_image_path) watermark = Image.open(watermark_image_path).convert("RGBA") # add watermark to your image position = base_image.size newsize = (int(position[0]*8/100),int(position[0]*8/100)) # print(position) watermark = watermark.resize(newsize) # print(newsize) # return watermark

new_position = position[0]-newsize[0]-20,position[1]-newsize[1]-20 # create a new transparent image transparent = Image.new(mode='RGBA',size=position,color=(0,0,0,0)) # paste the original image transparent.paste(base_image,(0,0)) # paste the watermark image transparent.paste(watermark,new_position,watermark) image_mode = base_image.mode print(image_mode) if image_mode == 'RGB': transparent = transparent.convert(image_mode) else: transparent = transparent.convert('P') transparent.save(output_image_path,optimize=True,quality=100) print("Saving"+output_image_path+"...")

folder = input("Enter Folder Path:")``watermark = input("Enter Watermark Path:")``os.chdir(folder)``files = os.listdir(os.getcwd())``print(files)

if not os.path.isdir("output"): os.mkdir("output")

c = 1``for f in files: if os.path.isfile(os.path.abspath(f)): if f.endswith(".png") or f.endswith(".jpg"): `watermark_photo(f,watermark,“output/”+f)

6.从WEB页面报废并下载所有图像


from selenium import webdriverimport requests as rqimport osfrom bs4 import BeautifulSoupimport time`

# path= E:\web scraping\chromedriver_win32\chromedriver.exe``path = input("Enter Path : ")

url = input("Enter URL : ")

output = "output"

def get_url(path, url): driver = webdriver.Chrome(executable_path=r"{}".format(path)) driver.get(url) print("loading.....") res = driver.execute_script("return document.documentElement.outerHTML")

return res

def get_img_links(res): soup = BeautifulSoup(res, "lxml") imglinks = soup.find_all("img", src=True) return imglinks

def download_img(img_link, index): try: extensions = [".jpeg", ".jpg", ".png", ".gif"] extension = ".jpg" for exe in extensions: if img_link.find(exe) > 0: extension = exe break

img_data = rq.get(img_link).content with open(output + "\\" + str(index + 1) + extension, "wb+") as f: f.write(img_data)

f.close() except Exception: pass

result = get_url(path, url)``time.sleep(60)``img_links = get_img_links(result)``if not os.path.isdir(output): os.mkdir(output)

for index, img_link in enumerate(img_links): img_link = img_link["src"] print("Downloading...") if img_link: `download_img(img_link, index)``print(“Download Complete!!”)

7.低电量通知


​​​​​​​

pip install psutil``import psutil`

battery = psutil.sensors_battery()``plugged = battery.power_plugged``percent = battery.percent

if percent <= 30 and plugged!=True:

# pip install py-notifier # pip install win10toast from pynotifier import Notification

Notification( title="Battery Low", description=str(percent) + "% Battery remain!!", duration=5, # Duration in seconds

`).send()

8.计算你的年龄


​​​​​​​

import time``from calendar import isleap`

# judge the leap year``def judge_leap_year(year): if isleap(year): return True else: return False

# returns the number of days in each month``def month_days(month, leap_year): if month in [1, 3, 5, 7, 8, 10, 12]: return 31 elif month in [4, 6, 9, 11]: return 30 elif month == 2 and leap_year: return 29 elif month == 2 and (not leap_year): return 28

name = input("input your name: ")``age = input("input your age: ")``localtime = time.localtime(time.time())

year = int(age)``month = year * 12 + localtime.tm_mon``day = 0

begin_year = int(localtime.tm_year) - year``end_year = begin_year + year

# calculate the days``for y in range(begin_year, end_year): if (judge_leap_year(y)): day = day + 366 else: day = day + 365

leap_year = judge_leap_year(localtime.tm_year)``for m in range(1, localtime.tm_mon): day = day + month_days(m, leap_year)

`day = day + localtime.tm_mdayprint("%s's age is %d years or " % (name, year), end="")print(“%d months or %d days” % (month, day))

9.有组织的不同类别的下载文件夹


​​​​​​​

​​​​​​​

import osimport shutilos.chdir(“E:\downloads”)``#print(os.getcwd())`

#check number of files in directory``files = os.listdir()

#list of extension (You can add more if you want)``extentions = { "images": [".jpg", ".png", ".jpeg", ".gif"], "videos": [".mp4", ".mkv"], "musics": [".mp3", ".wav"], "zip": [".zip", ".tgz", ".rar", ".tar"], "documents": [".pdf", ".docx", ".csv", ".xlsx", ".pptx", ".doc", ".ppt", ".xls"], "setup": [".msi", ".exe"], "programs": [".py", ".c", ".cpp", ".php", ".C", ".CPP"], "design": [".xd", ".psd"]

}

#sort to specific folder depend on extenstions``def sorting(file): keys = list(extentions.keys()) for key in keys: for ext in extentions[key]: # print(ext) if file.endswith(ext): return key

#iterat through each file``for file in files: dist = sorting(file) if dist: try: shutil.move(file, "../download-sorting/" + dist) except: print(file + " is already exist") else: try: shutil.move(file, "../download-sorting/others") except: `print(file + " is already exist")

10.从 CSV 文件批量发送电子邮件


​​​​​​​

import csv``from email.message import EmailMessage``import smtplib

def get_credentials(filepath): with open("credentials.txt", "r") as f: email_address = f.readline() email_pass = f.readline() return (email_address, email_pass)

def login(email_address, email_pass, s): s.ehlo() # start TLS for security s.starttls() s.ehlo() # Authentication s.login(email_address, email_pass) print("login")

def send_mail(): s = smtplib.SMTP("smtp.gmail.com", 587) email_address, email_pass = get_credentials("./credentials.txt") login(email_address, email_pass, s)

# message to be sent subject = "Welcome to Python" body = """Python is an interpreted, high-level, general-purpose programming language.\n Created by Guido van Rossum and first released in 1991, Python's design philosophy emphasizes code readability\n with its notable use of significant whitespace"""

message = EmailMessage() message.set_content(body) message['Subject'] = subject

with open("emails.csv", newline="") as csvfile: spamreader = csv.reader(csvfile, delimiter=" ", quotechar="|") for email in spamreader: s.send_message(email_address, email[0], message) print("Send To " + email[0])

# terminating the session s.quit() print("sent")

if __name__ == "__main__": send_mail()

11.获取网站的IP地址和主机名


Get Ipaddress and Hostname of Website# importing socket libraryimport socket`

def get_hostname_IP(): hostname = input("Please enter website address(URL):") try: print (f'Hostname: {hostname}') print (f'IP: {socket.gethostbyname(hostname)}') except socket.gaierror as error: print (f'Invalid Hostname, error raised is {error}')

`get_hostname_IP()

12.终端进度条


from tqdm import tqdm``from PIL import Image``import os``from time import sleep

def Resize_image(size, image): if os.path.isfile(image): try: im = Image.open(image) im.thumbnail(size, Image.ANTIALIAS) im.save("resize/" + str(image) + ".jpg") except Exception as ex: print(f"Error: {str(ex)} to {image}")

path = input("Enter Path to images : ")``size = input("Size Height , Width : ")``size = tuple(map(int, size.split(",")))

os.chdir(path)

list_images = os.listdir(path)``if "resize" not in list_images: os.mkdir("resize")

for image in tqdm(list_images, desc="Resizing Images"): Resize_image(size, image) sleep(0.1)``print("Resizing Completed!")

13. Wifi密码弹出器


import subprocess`

data = ( subprocess.check_output(["netsh", "wlan", "show", "profiles"]) .decode("utf-8") .split("\n")``)``profiles = [i.split(":")[1][1:-1] for i in data if "All User Profile" in i]``for i in profiles: results = ( subprocess .check_output(["netsh", "wlan", "show", "profile", i, "key=clear"]) .decode("utf-8") .split("\n") ) results = [b.split(":")[1][1:-1] for b in results if "Key Content" in b] try: print("{:<30}| {:<}".format(i, results[0])) except IndexError: `print(“{:<30}| {:<}”.format(i, “”))

14.给定网站的快照


import sysfrom selenium import webdriverfrom selenium.webdriver.chrome.options import Options``import chromedriver_binary`

script_name = sys.argv[0]

options = Options()``options.add_argument('--headless')``driver = webdriver.Chrome(options=options)

try: url = sys.argv[1]

driver.get(url) page_width = driver.execute_script('return document.body.scrollWidth') page_height = driver.execute_script('return document.body.scrollHeight') driver.set_window_size(page_width, page_height) driver.save_screenshot('screenshot.png') driver.quit() print("SUCCESS")

except IndexError: `print(‘Usage: %s URL’ % script_name)

15.将文件拆分成块​​​​​​​


import sysimport osimport shutil``import pandas as pd`

class Split_Files: ''' Class file for split file program ''' def __init__(self, filename, split_number): ''' Getting the file name and the split index Initializing the output directory, if present then truncate it. Getting the file extension ''' self.file_name = filename self.directory = "file_split" self.split = int(split_number) if os.path.exists(self.directory): shutil.rmtree(self.directory) os.mkdir(self.directory) if self.file_name.endswith('.txt'): self.file_extension = '.txt' else: self.file_extension = '.csv' self.file_number = 1

def split_data(self): ''' spliting the input csv/txt file according to the index provided ''' data = pd.read_csv(self.file_name, header=None) data.index += 1

split_frame = pd.DataFrame() output_file = f"{self.directory}/split_file{self.file_number}{self.file_extension}"

for i in range(1, len(data)+1): split_frame = split_frame.append(data.iloc[i-1]) if i % self.split == 0: output_file = f"{self.directory}/split_file{self.file_number}{self.file_extension}" if self.file_extension == '.txt': split_frame.to_csv(output_file, header=False, index=False, sep=' ') else: split_frame.to_csv(output_file, header=False, index=False) split_frame.drop(split_frame.index, inplace=True) self.file_number += 1 if not split_frame.empty: output_file = f"{self.directory}/split_file{self.file_number}{self.file_extension}" split_frame.to_csv(output_file, header=False, index=False)

if __name__ == '__main__': file, split_number = sys.argv[1], sys.argv[2] sp = Split_Files(file, split_number) `sp.split_data()

16.加密和解密文本


​​​​​​​

from Crypto.Cipher import AESfrom Crypto import Randomfrom binascii import b2a_hex``import sys`

# get the plaintext``plain_text = sys.argv[1]

# The key length must be 16 (AES-128), 24 (AES-192), or 32 (AES-256) Bytes.``key = b'this is a 16 key'

# Generate a non-repeatable key vector with a length``# equal to the size of the AES block``iv = Random.new().read(AES.block_size)

# Use key and iv to initialize AES object, use MODE_CFB mode``mycipher = AES.new(key, AES.MODE_CFB, iv)

# Add iv (key vector) to the beginning of the encrypted ciphertext``# and transmit it together``ciphertext = iv + mycipher.encrypt(plain_text.encode())

# To decrypt, use key and iv to generate a new AES object``mydecrypt = AES.new(key, AES.MODE_CFB, ciphertext[:16])

# Use the newly generated AES object to decrypt the encrypted ciphertext``decrypttext = mydecrypt.decrypt(ciphertext[16:])

# output``file_out = open("encrypted.bin", "wb")``file_out.write(ciphertext[16:])``file_out.close()

`print("The key k is: ", key)print("iv is: ", b2a_hex(ciphertext)[:16])print("The encrypted data is: ", b2a_hex(ciphertext)[16:])``print("The decrypted data is: ", decrypttext.decode())

17.定期截屏


​​​​​​​

最后

🍅 硬核资料:关注即可领取PPT模板、简历模板、行业经典书籍PDF。
🍅 技术互助:技术群大佬指点迷津,你的问题可能不是问题,求资源在群里喊一声。
🍅 面试题库:由技术群里的小伙伴们共同投稿,热乎的大厂面试真题,持续更新中。
🍅 知识体系:含编程语言、算法、大数据生态圈组件(Mysql、Hive、Spark、Flink)、数据仓库、Python、前端等等。

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加V获取:vip1024c (备注python)
img

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

🍅 硬核资料:关注即可领取PPT模板、简历模板、行业经典书籍PDF。
🍅 技术互助:技术群大佬指点迷津,你的问题可能不是问题,求资源在群里喊一声。
🍅 面试题库:由技术群里的小伙伴们共同投稿,热乎的大厂面试真题,持续更新中。
🍅 知识体系:含编程语言、算法、大数据生态圈组件(Mysql、Hive、Spark、Flink)、数据仓库、Python、前端等等。

网上学习资料一大堆,但如果学到的知识不成体系,遇到问题时只是浅尝辄止,不再深入研究,那么很难做到真正的技术提升。

需要这份系统化的资料的朋友,可以添加V获取:vip1024c (备注python)
[外链图片转存中…(img-QksyJDtL-1713276216413)]

一个人可以走的很快,但一群人才能走的更远!不论你是正从事IT行业的老鸟或是对IT行业感兴趣的新人,都欢迎加入我们的的圈子(技术交流、学习资源、职场吐槽、大厂内推、面试辅导),让我们一起学习成长!

  • 3
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值