Python 代码:去除文件名中的括号
以下 Python 代码可以去除文件名中的括号:
import os
def remove_parentheses(filename):
'Removes parentheses from filename.'
return filename.replace('(', '').replace(')', '')
# 示例使用
path = 'path/to/directory'
for filename in os.listdir(path):
new_filename = remove_parentheses(filename)
os.rename(os.path.join(path, filename), os.path.join(path, new_filename))
这段代码使用了 Python 标准库中的 os 模块,可以列出指定目录下的所有文件,然后使用 os.rename 函数重命名文件。函数 remove_parentheses 使用字符串的 replace 方法去掉文件名中的括号。
原文地址: https://www.cveoy.top/t/topic/m31H 著作权归作者所有。请勿转载和采集!