Python脚本使用正则替换文件名中|字符为l时未生效的问题排查
Hey there, let's walk through why your script isn't renaming those files—there are a few key issues here that are keeping it from working as expected:
Key Problems in Your Script
You're modifying the string representation of the entire list, not individual filenames
When you runlist = str(list_file), you're turning the whole list of filenames into a single string (like"['file|name.txt', 'another|file.jpg']") instead of iterating over each file one by one. There.subcall only changes this big string, not the actual filenames on your disk. Also, avoid usinglistas a variable name—it's a built-in Python type, and overwriting it can cause unexpected bugs.Your regex pattern is incorrect for the task
The pattern[\\":<>;|*?]*matches any combination of those special characters (including zero occurrences) and replaces all of them with "l". But your goal is only to swap|withl. For this simple replacement, you don't even need regex—Python's built-instr.replace()method is cleaner and more efficient. If you did want to use regex, the pattern should just be\|(escaped because|is a special regex character).You're not saving the result of
re.subre.subreturns a new string (it doesn't modify the original in-place), so you need to assign its output to a variable to actually use the modified text.You never actually rename the files
Even if you processed the filenames correctly, you're missing the critical step: callingos.rename()to update the actual file names on your filesystem.
Fixed Script
Here's a revised version that addresses all these issues and works as intended:
#!/usr/bin/python import os class bcolors: HEADER = '\033[95m' OKBLUE = '\033[94m' OKCYAN = '\033[96m' OKGREEN = '\033[92m' WARNING = '\033[93m' FAIL = '\033[91m' ENDC = '\033[0m' BOLD = '\033[1m' UNDERLINE = '\033[4m' # Get the directory where the script is located file_src = os.path.dirname(os.path.abspath(__file__)) print(bcolors.OKBLUE + bcolors.BOLD + 'Directory:', file_src + bcolors.ENDC) print() # Iterate over each entry in the directory for filename in os.listdir(file_src): # Skip the script file itself to avoid renaming it if filename == os.path.basename(__file__): continue # Optional: Skip directories if you only want to rename files if not os.path.isfile(os.path.join(file_src, filename)): print(bcolors.WARNING + f"Skipping directory: {filename}" + bcolors.ENDC) continue # Replace | with l (simple string swap is perfect here) new_filename = filename.replace('|', 'l') # Only rename if the filename actually changed if new_filename != filename: old_path = os.path.join(file_src, filename) new_path = os.path.join(file_src, new_filename) os.rename(old_path, new_path) print(bcolors.OKGREEN + f"Renamed: {filename} -> {new_filename}" + bcolors.ENDC) else: print(bcolors.WARNING + f"No change needed: {filename}" + bcolors.ENDC)
Quick Notes on the Fixed Code
- Uses
os.path.join()to safely build file paths (works across Windows, macOS, and Linux) - Skips renaming the script itself to avoid breaking it
- Optional check to skip directories (remove that block if you want to rename folders too)
- Only runs the rename operation if the filename actually has a
|to replace (avoids unnecessary work) - Uses
str.replace()instead of regex for this straightforward character swap
内容的提问来源于stack exchange,提问作者Trunks

