我喜欢这个 -文件重命名/删除
* .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