该问题与有关递归重命名文件的答案有关。
更改为替换破折号的代码不适用于以下情况:
./Beginners Tools/Hello's -Trojans-/bif43243
./Linux/Nux Col - 1 Works (TEX & Pdf) - T'eouhsoe & More (33323 - 34432)
./Git/peepcode-git-mov/c6_branch_merge.mov
./haskell/OS 2007 - T aoue
./B Sites for Get-Big
它适用于以下案例:
./oeu'oeu - X ee ls - Feb 2008.pdf
所以我需要解析数据。如何正确替换破折号?
[细节]
代码来自链接,但更改为替换“-”:
import os
for dirpath, dirs, files in os.walk(your_path):
for filename in files:
if '&' in filename:
os.rename(
os.path.join(dirpath, filename),
os.path.join(dirpath, filename.replace('-', '_'))
)
Python 并没有取代每一个破折号。我认为这是因为名称包含之前停止脚本的特殊标志。所以我在归档时遇到了错误:
tar cvzf sed_backup.tar.gz `find documents | sed s/\.*/\'\&\'/`
tar: rojans-: Cannot stat: No such file or directory
tar: Error is not recoverable: exiting now
由于名称中仍保留符号“'”和“-”,tar 命令将“'”解释为 find-command 的结尾,将“-”解释为路径中的选项符号“./Beginners Tools/你好-Trojans-/bif43243"