递归批量修改文件后缀

2015-09-10 11:34:38 +08:00
 superwg1984
*代码如下*
------
import os, re, string

path = "/Users/wanggeng/work/html/mygame/view/"


def ppp (path, file ):
if os.path.isfile (path + file ):
portion = os.path.splitext (file )
newname = portion[0] + ".html"
print (file, newname )
os.rename (path + file, path + newname )
print portion
else:
newpath = path + file + "/"
files = os.listdir (newpath )
for f in files:
ppp (newpath, f )


ppp (path, "")
2576 次点击
所在节点    Python
7 条回复
ChiChou
2015-09-10 12:50:50 +08:00
用 shell 不是更方便? 0 0
superwg1984
2015-09-10 12:56:49 +08:00
@ChiChou 别提了,我现学 shell,俩小时没弄出来...越来越不喜欢 shell 了
fuge
2015-09-10 13:44:10 +08:00
find | xargs
ChiChou
2015-09-10 14:38:41 +08:00
find . -type f -iname "*.htm" -print0 | while IFS= read -r -d $'\0' line; do mv "$line" "${line%.*}".html; done;
omph
2015-09-10 19:50:19 +08:00
shell 比较灵活,楼主没找对方向
find "$path" -type f -execdir rename 's/.[\w]+$/.html/' '{}' +
kaisfm
2015-09-16 12:01:14 +08:00
有一个东东叫 walker
kaisfm
2015-09-16 12:08:26 +08:00
@kaisfm os.walk 、 os.path.walk

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/219598

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX