V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
superwg1984
V2EX  ›  Python

递归批量修改文件后缀

  •  
  •   superwg1984 ·
    superwg1984 · 2015-09-10 11:34:38 +08:00 · 2557 次点击
    这是一个创建于 3144 天前的主题,其中的信息可能已经有所发展或是发生改变。

    代码如下

    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, "")
    
    7 条回复    2015-09-16 12:08:26 +08:00
    ChiChou
        1
    ChiChou  
       2015-09-10 12:50:50 +08:00
    用 shell 不是更方便? 0 0
    superwg1984
        2
    superwg1984  
    OP
       2015-09-10 12:56:49 +08:00
    @ChiChou 别提了,我现学 shell,俩小时没弄出来...越来越不喜欢 shell 了
    fuge
        3
    fuge  
       2015-09-10 13:44:10 +08:00
    find | xargs
    ChiChou
        4
    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
        5
    omph  
       2015-09-10 19:50:19 +08:00
    shell 比较灵活,楼主没找对方向
    find "$path" -type f -execdir rename 's/.[\w]+$/.html/' '{}' +
    kaisfm
        6
    kaisfm  
       2015-09-16 12:01:14 +08:00
    有一个东东叫 walker
    kaisfm
        7
    kaisfm  
       2015-09-16 12:08:26 +08:00   ❤️ 1
    @kaisfm os.walk 、 os.path.walk
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   2833 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 27ms · UTC 14:21 · PVG 22:21 · LAX 07:21 · JFK 10:21
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.