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
添加回答
舉報