Batch Renaming of Files in a Directory

前端 未结 13 1495
孤街浪徒
孤街浪徒 2020-11-27 09:41

Is there an easy way to rename a group of files already contained in a directory, using Python?

Example: I have a directory full of *.doc files an

13条回答
  •  心在旅途
    2020-11-27 09:53

    I prefer writing small one liners for each replace I have to do instead of making a more generic and complex code. E.g.:

    This replaces all underscores with hyphens in any non-hidden file in the current directory

    import os
    [os.rename(f, f.replace('_', '-')) for f in os.listdir('.') if not f.startswith('.')]
    

提交回复
热议问题