我喜歡這個 -文件重命名/刪除
* .sss幾個文件和* _passive.sss
如果我有一個文件名爲blah1.sss和blah1_passive.sss,我想擺脫blah1.sss並重命名爲blah1_passive.sss。但是,如果我沒有blah1_passive.sss,我想記錄文件名並保留blah1.sss。
我能夠找到所有* _passive.sss文件,但我想知道可以將* _passive.sss重命名爲* .sss的awk/sed etc命令。
編輯:現在我有這個,但os.rename不覆蓋文件,我需要它覆蓋,但。
import os, fnmatch
def locate(pattern, root=os.curdir):
#ignore directories- uncomment to make ignore work
#ignored = ["0201", "0306"]
for path, dirs, files in os.walk(os.path.abspath(root)):
#for dir in ignored: # if dir in dirs: #dirs.remove(dir)
for filename in fnmatch.filter(files, pattern):
yield os.path.join(path, filename)
for filename in locate("*_passive.sss"):
#found the files that i want to rename, but os.rename() refuses to overwrite !!
newfilename=filename.rstrip("_passive.sss") + ".sss"
os.rename(filename,newfilename)
嘗試'os.remove(newfilename); os.rename(filename,newfilename)' – neurino 2011-05-16 22:15:53