update bug
This commit is contained in:
@@ -6,14 +6,29 @@ def get_random_file_from_dir(directory):
|
||||
image_extensions = {'.jpg', '.jpeg', '.png', '.gif', '.bmp', '.tiff', '.webp'}
|
||||
image_files = []
|
||||
|
||||
with os.scandir(directory) as entries:
|
||||
for entry in entries:
|
||||
if entry.is_file():
|
||||
_, ext = os.path.splitext(entry.name)
|
||||
if ext.lower() in image_extensions:
|
||||
image_files.append(entry.path)
|
||||
if not os.path.exists(directory):
|
||||
print(f"Error: Directory '{directory}' does not exist.")
|
||||
return None
|
||||
|
||||
return random.choice(image_files) if image_files else None
|
||||
if not os.access(directory, os.R_OK):
|
||||
print(f"Error: No read access to directory '{directory}'.")
|
||||
return None
|
||||
|
||||
print(f"Scanning directory: {directory} (including subdirectories)")
|
||||
|
||||
# 使用 os.walk() 递归遍历所有子目录
|
||||
for root, _, files in os.walk(directory):
|
||||
for file in files:
|
||||
_, ext = os.path.splitext(file)
|
||||
if ext.lower() in image_extensions:
|
||||
full_path = os.path.join(root, file)
|
||||
image_files.append(full_path)
|
||||
|
||||
if not image_files:
|
||||
print("No image files found in the directory (including subdirectories).")
|
||||
return None
|
||||
|
||||
return random.choice(image_files)
|
||||
|
||||
|
||||
def get_xiuren_pic():
|
||||
@@ -34,4 +49,3 @@ def get_xiuren_heisi_pic():
|
||||
return os.path.abspath(random_file_path)
|
||||
else:
|
||||
return None
|
||||
|
||||
|
||||
Reference in New Issue
Block a user