3 回答
TA貢獻(xiàn)1785條經(jīng)驗(yàn) 獲得超4個(gè)贊
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
您還可以使用 進(jìn)行批量重命名以提高速度xargs,例如
find ... -print0 | xargs -0 ...
但就您而言,我認(rèn)為逐個(gè)重命名文件已經(jīng)足夠快了。
命令行實(shí)用程序rename有多種形式。他們中的大多數(shù)人應(yīng)該為這項(xiàng)任務(wù)而努力。我使用renameAristotle Pagaltzis 的 1.601 版本。要安裝rename,只需下載其 Perl 腳本并將其放入$PATH. 或者rename使用安裝conda,如下所示:
conda install rename
TA貢獻(xiàn)1719條經(jīng)驗(yàn) 獲得超6個(gè)贊
您可以從嘗試這個(gè) pyhon 3 腳本開(kāi)始。不過(guò)我只在 Windows 中測(cè)試過(guò)。
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貢獻(xiàn)1831條經(jīng)驗(yàn) 獲得超4個(gè)贊
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
添加回答
舉報(bào)
