How do I rename files in sub directories?

前端 未结 15 1434
臣服心动
臣服心动 2020-12-04 11:10

Is there any way of batch renaming files in sub directories?

For example:

Rename *.html to *.htm in a folder which has directories

15条回答
  •  庸人自扰
    2020-12-04 11:44

    In python

    import os
    
    target_dir = "."
    
    for path, dirs, files in os.walk(target_dir):
        for file in files:
            filename, ext = os.path.splitext(file)
            new_file = filename + ".htm"
    
            if ext == '.html':
                old_filepath = os.path.join(path, file)
                new_filepath = os.path.join(path, new_file)
                os.rename(old_filepath, new_filepath)
    

提交回复
热议问题