Wenn Sie Bilder per Drag-and-Drop in einen lokalen Ordner im Chrome-Browser ziehen, die Namen jedoch verwirrend sind oder denselben Namen haben, besteht dieses Problem nicht, wenn wir alle lokalen Bilder richtig benennen.
Verwenden Sie Python, um ein Programm zu schreiben, das den Dateinamen (ohne Suffix) in C:\Benutzer\xjsd\Desktop\outpainting_test_data kontinuierlich überwacht. Wenn die Benennung nicht f0001 entspricht [0001 ist inkrementell], ändern Sie den Dateinamen in einen solchen entspricht der Spezifikation. (Das Suffix bleibt unverändert).
Python
import os
import re
import time
def rename_files(directory_path):
# Get a list of existing file names in the directory
existing_files = [f for f in os.listdir(directory_path) if os.path.isfile(os.path.join(directory_path, f))]
# Sort the existing file names
existing_files.sort()
# Initialize a counter for the new names
counter = len(existing_files)
# Iterate through existing files
for filename in existing_files:
base_name, file_extension = os.path.splitext(filename)
# Define the expected pattern f0001
pattern = re.compile(r'f(\d{4})')
# Check if the current filename matches the pattern
match = re.match(pattern, base_name)
if not match:
while 1:
# If not, generate a new filename following the pattern
new_filename = f'f{
str(counter).zfill(4)}{
file_extension}'
# Update the counter
counter += 1
# Check if the new filename already exists
if new_filename not in existing_files:
break
# Rename the file
old_file_path = os.path.join(directory_path, filename)
new_file_path = os.path.join(directory_path, new_filename)
os.rename(old_file_path, new_file_path)
print(f'Renamed: {
filename} -> {
new_filename}')
# Replace 'C:\\Users\\xjsd\\Desktop\\outpainting_test_data' with your actual directory path
directory_path = r'C:\Users\xjsd\Desktop\outpainting_test_data'
while 1:
# Run the rename_files function
rename_files(directory_path)
time.sleep(1)