def list2str(lst):
"""
Turn a list into a string
Arguments:
- `lst`: Input list.
"""
string = ""
for item in lst:
string += item+"."
return string.rstrip(".")
def rename_single(arg, dirname, filenames):
"""
Rename single file.
Arguments:
- `arg`:
- `dirname`: Directory name
- `filenames`: File name list.
"""
print os.getcwd()
for fn in filenames:
if os.path.isdir(fn):
continue # Skip direcoty.
old_name = os.path.join(dirname, fn);
bname = list2str(fn.split(".")[:-1])
ename = fn.split(".")[-1]
if not len(bname): # This file does not have extension name
bname = ename
ename = ""
new_name = os.path.join(dirname, bname+"_"+os.getenv("USER"))
else:
new_name = os.path.join(dirname,
bname+"_"+os.getenv("USER")+"."+ename)
try:
print "Rename %s to %s"%(old_name, new_name)
os.rename(old_name, new_name)
except:
print "Failed to rename file: %s, please rename it manually"%\
old_name
print sys.exc_info()[1]
else:
print "Succceded in rename file %s"%old_name
if __name__ == '__main__':
os.path.walk(".", rename_single, None)
[/code]下面是执行结果:[code]yyc@tubo test $ python rename.py
/home/yyc/tmp/test
Rename ./a.b.txt to ./a.b_yyc.txt
Succceded in rename file ./a.b.txt
Rename ./c.txt to ./c_yyc.txt
Succceded in rename file ./c.txt
Rename ./a.txt to ./a_yyc.txt
Succceded in rename file ./a.txt
Rename ./rename.py to ./rename_yyc.py
Succceded in rename file ./rename.py
Rename ./b.txt to ./b_yyc.txt
Succceded in rename file ./b.txt
Rename ./a to ./a_yyc
Succceded in rename file ./a
[/code]Hope it helps.
Refer to the following example.[code]yyc@localhost ~/tmp/test $ ls
a b c d e f g h
yyc@localhost ~/tmp/test $ for fn in *; do mv $fn $fn\[$USER\];done
yyc@localhost ~/tmp/test $ ls
a[yyc] b[yyc] c[yyc] d[yyc] e[yyc] f[yyc] g[yyc] h[yyc]
[/code]其中 yyc 是我的用户名,在你的机器上会由 $USER 自动替换成你的。
deepwhite 于 2010-12-22 08:33:59发表:
将的下面的python 脚本保存为 rename.py ,放在想要修改的目录下,然后进入该目录,执行 : python rename.py 即可。
现在这个脚本会修改目录以及子目录下的所有文件,由于时间关系我没有乍进一步改进,有兴趣的话可以自己改改,加入depth 限制,或
者直接从 cmdline 传入待修改的目录等等。[code]
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os
import sys
def list2str(lst):
"""
Turn a list into a string
Arguments:
- `lst`: Input list.
"""
string = ""
for item in lst:
string += item+"."
return string.rstrip(".")
def rename_single(arg, dirname, filenames):
"""
Rename single file.
Arguments:
- `arg`:
- `dirname`: Directory name
- `filenames`: File name list.
"""
print os.getcwd()
for fn in filenames:
if os.path.isdir(fn):
continue # Skip direcoty.
old_name = os.path.join(dirname, fn);
bname = list2str(fn.split(".")[:-1])
ename = fn.split(".")[-1]
if not len(bname): # This file does not have extension name
bname = ename
ename = ""
new_name = os.path.join(dirname, bname+"_"+os.getenv("USER"))
else:
new_name = os.path.join(dirname,
bname+"_"+os.getenv("USER")+"."+ename)
try:
print "Rename %s to %s"%(old_name, new_name)
os.rename(old_name, new_name)
except:
print "Failed to rename file: %s, please rename it manually"%\
old_name
print sys.exc_info()[1]
else:
print "Succceded in rename file %s"%old_name
if __name__ == '__main__':
os.path.walk(".", rename_single, None)
[/code]下面是执行结果:[code]yyc@tubo test $ python rename.py
/home/yyc/tmp/test
Rename ./a.b.txt to ./a.b_yyc.txt
Succceded in rename file ./a.b.txt
Rename ./c.txt to ./c_yyc.txt
Succceded in rename file ./c.txt
Rename ./a.txt to ./a_yyc.txt
Succceded in rename file ./a.txt
Rename ./rename.py to ./rename_yyc.py
Succceded in rename file ./rename.py
Rename ./b.txt to ./b_yyc.txt
Succceded in rename file ./b.txt
Rename ./a to ./a_yyc
Succceded in rename file ./a
[/code]Hope it helps.
and you need python 2.6 to excute this script.
飞飞海 于 2010-12-21 14:54:22发表:
5# deepwhite
好像还有点缺陷 出来的效果 a.txt[owner] 怎么写是a[owner].txt?
飞飞海 于 2010-12-21 12:58:16发表:
5# deepwhite
谢谢斑竹大大
cainiaogaofei 于 2010-12-20 10:26:20发表:
我是新手,我要交流,我要赚现金!
deepwhite 于 2010-12-20 08:52:48发表:
Refer to the following example.[code]yyc@localhost ~/tmp/test $ ls
a b c d e f g h
yyc@localhost ~/tmp/test $ for fn in *; do mv $fn $fn\[$USER\];done
yyc@localhost ~/tmp/test $ ls
a[yyc] b[yyc] c[yyc] d[yyc] e[yyc] f[yyc] g[yyc] h[yyc]
[/code]其中 yyc 是我的用户名,在你的机器上会由 $USER 自动替换成你的。
相思爱文 于 2010-12-19 17:33:07发表:
[i=s] 本帖最后由 相思爱文 于 2010-12-19 17:41 编辑 [/i]
批量重命名就行。
用google搜索“linux批量重命名”
飞飞海 于 2010-12-19 13:57:07发表:
:0w223dc 高手来帮帮忙啊~~
水君 于 2010-12-19 13:51:31发表:
我也想知道哦