V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
推荐学习书目
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 · Sep 10, 2015 · 3582 views
    This topic created in 3885 days ago, the information mentioned may be changed or developed.

    代码如下

    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 replies    2015-09-16 12:08:26 +08:00
    ChiChou
        1
    ChiChou  
       Sep 10, 2015
    用 shell 不是更方便? 0 0
    superwg1984
        2
    superwg1984  
    OP
       Sep 10, 2015
    @ChiChou 别提了,我现学 shell,俩小时没弄出来...越来越不喜欢 shell 了
    fuge
        3
    fuge  
       Sep 10, 2015
    find | xargs
    ChiChou
        4
    ChiChou  
       Sep 10, 2015
    find . -type f -iname "*.htm" -print0 | while IFS= read -r -d $'\0' line; do mv "$line" "${line%.*}".html; done;
    omph
        5
    omph  
       Sep 10, 2015
    shell 比较灵活,楼主没找对方向
    find "$path" -type f -execdir rename 's/.[\w]+$/.html/' '{}' +
    kaisfm
        6
    kaisfm  
       Sep 16, 2015
    有一个东东叫 walker
    kaisfm
        7
    kaisfm  
       Sep 16, 2015   ❤️ 1
    @kaisfm os.walk 、 os.path.walk
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2558 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 34ms · UTC 02:10 · PVG 10:10 · LAX 19:10 · JFK 22:10
    ♥ Do have faith in what you're doing.