Pythonバッチ名前変更ファイル(例として画像ファイル)

画像ファイルの名前をバッチで変更

Pythonに付属のosパッケージを使用すると、** rename()**関数を使用してファイルの名前を変更できます。

深層学習資料の場合、画像資料はさまざまなソースから取得される可能性があり、元のファイル名は大きく異なります(ファイル形式も異なる場合があります)。

この記事では、画像ファイルの名前を変更する方法について説明します。** endswith()**と組み合わせて、「。png」、「。jpg」などのファイル名を決定します。画像形式の変換に関しては、OpenCV-Pythonの関連する操作を組み合わせて自分で処理し、ファイル名とファイル形式の統一を実現できます。

コードを見せて

import os

def renamefiles(pathName):
    i = 0
    for fileName in os.listdir(pathName):# listing all the files in the directory
        # don't rename the unrelated files
        if fileName.endswith('jpg'): # or fileName.endswith('png'):
            #os.path.join: combine the path with file name
            os.rename(os.path.join(pathName,fileName),os.path.join(pathName,(str(i)+'.jpg')))#or .png
        i+=1
    print('Rename finished...')
if __name__=="__main__":
    # current directory
    pathName='./'
    renamefiles(pathName)

おすすめ

転載: blog.csdn.net/weixin_44278406/article/details/108621421
おすすめ