LoginSignup
0
0

More than 3 years have passed since last update.

ファイル分類()

Posted at

ファイルを分類するために作ったものですが、
すごく個人的な目的のため,特に参考になりません。

以下、スクリプトです。

import shutil
import os
import pathlib
import string

##実際は何らかの形で引数として割り当てる
#voicefile ='namefileの絶対パス'

#実行ファイルを分類したいフォルダ内に移動して実行
voicefile = './'

#カレントディパレクトリを音声データがまとまって入っているフォルダにする
os.chdir(voicefile) 

ki='01-25'
gu='26-50'

#まとめる用のフォルダを作成
for i in range(9):
     #chi(97)=a

      makefilename =chr(i+97)+ki
      os.makedirs(makefilename, exist_ok=True)
      makefilename =chr(i+97)+gu
      os.makedirs(makefilename, exist_ok=True)

i=9
makefilename =chr(i+97)+ki
os.makedirs(makefilename, exist_ok=True)
makefilename =chr(i+97)+'26-53'
os.makedirs(makefilename, exist_ok=True)


# WAVファイルがあるフォルダの名前と同じ名前のフォルダを
# 作業ディレクトリから見つけ、コピー
def process(file_path):

        #WAVファイルが入っているフォルダ名(ディレクトリ)を取得
        dirname = os.path.dirname(file_path)

        #下6桁(a01-25etc.)を抽出
        folder_name =  dirname[-6:]
        #voicefile+'/フォルダ名'のディレクトリにコピー
        shutil.copy(file_path, voicefile+folder_name)


def recursive_file_check(path):
    if os.path.isdir(path):
        # directoryだったら中のファイルに対して再帰的にこの関数を実行
        files = os.listdir(path)
        for file in files:
            recursive_file_check(path + "/" + file)
    else:
        if path[-4:] == '.wav':
        # WAVfileだったら処理
         process(path)

recursive_file_check(voicefile)

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0