一、文件批量重命名
功能:将指定文件夹下的所有文件按照一定规则进行重命名。 使用方法:
import os
def batch_rename(folder_path, prefix):
files = os.listdir(folder_path)
for index, file_name in enumerate(files):
old_file_path = os.path.join(folder_path, file_name)
new_file_name = f"{prefix}_{index}.txt"
new_file_path = os.path.join(folder_path, new_file_name)
os.rename(old_file_path, new_file_path)
folder_path = "your_folder_path"
prefix = "new_name"
batch_rename(folder_path, prefix)
将your_folder_path替换为实际的文件夹路径,prefix设置为新文件名的前缀。
二、统计文件行数
功能:统计指定文件中的行数。 使用方法:
def count_lines(file_path): with open(file_path, 'r') as file: lines = file.readlines() return len(lines) file_path = "your_file.txt" print(count_lines(file_path))
将your_file.txt替换为要统计行数的文件路径。
三、提取文本中的特定内容
功能:从给定文本中提取特定的关键字或模式。 使用方法:
import re text = "This is a sample text with some keywords." pattern = r"keywords" matches = re.findall(pattern, text) print(matches)
根据实际需求修改text和pattern。
四、生成随机密码
功能:生成指定长度的随机密码。 使用方法:
import random import string def generate_password(length): characters = string.ascii_letters + string.digits + string.punctuation password = ''.join(random.choice(characters) for _ in range(length)) return password length = 10 print(generate_password(length))
设置length为所需密码的长度。
五、图片格式转换
功能:将一种图片格式转换为另一种格式。 使用方法:
from PIL import Image def convert_image(input_path, output_path, output_format): img = Image.open(input_path) img.save(output_path, format=output_format) input_path = "input.jpg" output_path = "output.png" output_format = "PNG" convert_image(input_path, output_path, output_format)
修改input_path、output_path和output_format为实际的路径和格式。
六、获取当前日期时间
功能:获取当前的日期和时间。 使用方法:
from datetime import datetime now = datetime.now() print(now)
七、计算两个日期之间的天数
功能:计算两个给定日期之间的天数差。 使用方法:
from datetime import date date1 = date(2023, 1, 1) date2 = date(2023, 1, 10) diff = date2 - date1 print(diff.days)
修改date1和date2为实际的日期。
八、文本文件内容去重
功能:去除文本文件中的重复行。 使用方法:
def remove_duplicates(file_path): with open(file_path, 'r') as file: lines = file.readlines() unique_lines = list(set(lines)) with open(file_path, 'w') as file: file.writelines(unique_lines) file_path = "your_file.txt" remove_duplicates(file_path)
九、检查文件是否存在
功能:检查指定路径的文件是否存在。 使用方法:
import os
file_path = "your_file.txt"
if os.path.exists(file_path):
print(f"{file_path} exists.")
else:
print(f"{file_path} does not exist.")
十、合并多个文本文件
功能:将多个文本文件的内容合并到一个文件中。 使用方法:
def merge_files(input_files, output_file): with open(output_file, 'w') as outfile: for file_name in input_files: with open(file_name, 'r') as infile: outfile.write(infile.read()) input_files = ["file1.txt", "file2.txt"] output_file = "merged.txt" merge_files(input_files, output_file)
修改input_files和output_file为实际的文件列表和输出文件名。
十一、获取文件夹大小
功能:计算指定文件夹的大小。 使用方法:
import os def get_folder_size(folder_path): total_size = 0 for dirpath, dirnames, filenames in os.walk(folder_path): for f in filenames: fp = os.path.join(dirpath, f) total_size += os.path.getsize(fp) return total_size folder_path = "your_folder_path" print(get_folder_size(folder_path))
十二、生成随机字符串
功能:生成指定长度的随机字符串。 使用方法:
import random import string def random_string(length): letters = string.ascii_lowercase return ''.join(random.choice(letters) for _ in range(length)) length = 8 print(random_string(length))
十三、计算文件的哈希值
功能:计算文件的 MD5 或其他哈希值。 使用方法:
import hashlib def hash_file(file_path): hash_md5 = hashlib.md5() with open(file_path, "rb") as f: for chunk in iter(lambda: f.read(4096), b""): hash_md5.update(chunk) return hash_md5.hexdigest() file_path = "your_file.txt" print(hash_file(file_path))
十四、转换大小写
功能:将文本中的字母转换为大写或小写。 使用方法:
text = "Hello, WORLD!" lowercase_text = text.lower() uppercase_text = text.upper() print(lowercase_text) print(uppercase_text)
十五、分割文本文件
功能:将一个大的文本文件分割成多个小文件。 使用方法:
def split_file(input_file, lines_per_file):
with open(input_file, 'r') as infile:
line_count = 0
file_number = 1
outfile = None
for line in infile:
if line_count % lines_per_file == 0:
if outfile:
outfile.close()
outfile_name = f"split_{file_number}.txt"
outfile = open(outfile_name, 'w')
file_number += 1
outfile.write(line)
line_count += 1
if outfile:
outfile.close()
input_file = "big_file.txt"
lines_per_file = 1000
split_file(input_file, lines_per_file)
十六、获取 IP 地址
功能:获取本地计算机的 IP 地址。 使用方法:
import socket def get_ip_address(): hostname = socket.gethostname() ip_address = socket.gethostbyname(hostname) return ip_address print(get_ip_address())
十七、提取网页标题
功能:从给定的 URL 中提取网页的标题。 使用方法:
import requests from bs4 import BeautifulSoup def get_page_title(url): response = requests.get(url) soup = BeautifulSoup(response.content, 'html.parser') title = soup.title.string return title url = "https://www.example.com" print(get_page_title(url))
十八、发送邮件
功能:使用 Python 发送电子邮件。 使用方法:
import smtplib
from email.mime.text import MIMEText
def send_email(sender, receiver, subject, body):
msg = MIMEText(body)
msg['Subject'] = subject
msg['From'] = sender
msg['To'] = receiver
smtp_server = smtplib.SMTP('smtp.example.com', 587)
smtp_server.starttls()
smtp_server.login(sender, 'your_password')
smtp_server.sendmail(sender, receiver, msg.as_string())
smtp_server.quit()
sender = "your_email@example.com"
receiver = "recipient@example.com"
subject = "Test Email"
body = "This is a test email."
send_email(sender, receiver, subject, body)
需要将smtp.example.com替换为实际的邮件服务器地址,并设置正确的发件人邮箱、密码等信息。
十九、计算平均值
功能:计算一组数字的平均值。 使用方法:
def average(numbers): total = sum(numbers) count = len(numbers) return total / count numbers = [1, 2, 3, 4, 5] print(average(numbers))
二十、检查字符串是否为数字
功能:判断一个字符串是否可以转换为数字。 使用方法:
def is_number(s): try: float(s) return True except ValueError: return False s = "123.45" print(is_number(s))


![[SICTF Round4] PWN](https://i-blog.csdnimg.cn/direct/f89f13b36a004e9f9b1bf7a921434390.png)
















