3 回答
TA贡献1785条经验 获得超4个赞
find ... -exec rename像这样使用:
find . -name "*[;']*" -exec rename "tr/';//d" {} \;
例子:
# Create example input files:
$ touch "f'o''o'" "b;a;;r;" "b';a;'';z;'"
# Build the command by first confirming that `find` finds them all:
$ find . -name "*[;']*"
./f'o''o'
./b';a;'';z;'
./b;a;;r;
# Find and rename them, one by one:
$ find . -name "*[;']*" -exec rename "tr/';//d" {} \;
# Confirm that rename worked as expected:
$ ls -1rt | tail -n 3
foo
bar
baz
您还可以使用 进行批量重命名以提高速度xargs,例如
find ... -print0 | xargs -0 ...
但就您而言,我认为逐个重命名文件已经足够快了。
命令行实用程序rename有多种形式。他们中的大多数人应该为这项任务而努力。我使用renameAristotle Pagaltzis 的 1.601 版本。要安装rename,只需下载其 Perl 脚本并将其放入$PATH. 或者rename使用安装conda,如下所示:
conda install rename
TA贡献1719条经验 获得超6个赞
您可以从尝试这个 pyhon 3 脚本开始。不过我只在 Windows 中测试过。
import os
folder = ""
for root, dirs, files in os.walk(folder, topdown=False):
for fn in files:
path_to_file = os.path.join(root, fn)
if "'" in fn or ";" in fn:
print('Removing special characters from file: ' + fn)
new_name = fn.replace("'", '').replace(";", '')
os.rename(path_to_file, os.path.join(root, new_name))
TA贡献1831条经验 获得超4个赞
import os
filesInDirectory = os.listdir(Path)
for filename in filesInDirectory:
if "'" in filename:
filename.replace("'", "")
elif ";" in filename:
filename.replace(";", "")
elif ("'" and ";") in filename:
filename.replace("'", "")
filename.replace(";", "")
使用Python
添加回答
举报