给4K群加入 #图来 指令。用于要色图
This commit is contained in:
25
xiuren/random_pic.py
Normal file
25
xiuren/random_pic.py
Normal file
@@ -0,0 +1,25 @@
|
||||
import os
|
||||
import random
|
||||
|
||||
|
||||
def get_random_file_from_dir(directory):
|
||||
selected_file = None
|
||||
file_count = 0
|
||||
|
||||
# 遍历目录及其所有子目录
|
||||
for root, dirs, files in os.walk(directory):
|
||||
for file in files:
|
||||
# 计算当前文件被选中的概率
|
||||
file_count += 1
|
||||
if random.randint(1, file_count) == 1:
|
||||
# 每个文件有1/file_count的概率被选中
|
||||
selected_file = os.path.join(root, file)
|
||||
|
||||
return selected_file
|
||||
|
||||
|
||||
def get_xiuren_pic():
|
||||
# 使用示例
|
||||
directory = '.' # 替换为你的目录路径
|
||||
random_file_path = get_random_file_from_dir(directory)
|
||||
return os.path.abspath(random_file_path)
|
||||
Reference in New Issue
Block a user