0

トレント ダウンロード フォルダーを整理するスクリプトを Python で作成しようとしています。私のフォルダー構造には、ファイルとファイルを含むフォルダー (複数のレベル) が含まれています。ファイルのファイル形式は異なりますが、フォルダーに MP4 タイプのファイルが含まれている場合は、ビデオ フォルダーに移動する必要があります。フォルダーに mp3/audioformat が含まれている場合は、Audiofolder に。最初のレベルのディレクトリのスクリプトを作成することができました。しかし、これを再帰的に行うのは大変です。グロブ、os.walk を試しました。glob は、fildir 名が原因でエラーを出します。そして、os.walkでは、サブディレクトリに特定のファイルタイプが含まれている場合、トップレベルのディレクトリを移動する方法が見つかりません。

    import os
    import shutil
    import glob
    numberFiles = 0
    totalObjects = 0
    numberDir= 0
    pathToDir = '/mybookone/seedbox1/files/' # this is the dir containing top lvl files and folders
    isEbook = 0
    isAudio = 0
    isAudioDir = 0
    bookExtensions = ('.epub', '.mobi','.pdf','.azw3','.djvu')
    audioExtensions = ('.m4b', '.mp3')
    ebookEndPath = '/mybookone/seedbox1/fileorgx/ebooks'
    audiobookEndPath = '/mybookone/seedbox1/fileorgx/audiobooks'
    
    for f in os.listdir(pathToDir):
        if (f == 'fileorgx'):
          continue
        totalObjects = totalObjects + 1
        if os.path.isfile(pathToDir + f):
            numberFiles = numberFiles + 1
            if(f.endswith(bookExtensions)):
              isEbook += 1
              shutil.move(pathToDir + f, ebookEndPath)
            if(f.endswith(audioExtensions)):
              isAudio += 1
              shutil.move(pathToDir + f, audiobookEndPath)
        if os.path.isdir(pathToDir + f):
            numberDir = numberDir + 1
            isAudioDir = 1  
            for ext in audioExtensions:
              pathFolderEsc = pathToDir
              for r,d,f in os.walk(pathToDir + f):
                for i in f:
                  print(os.path.join(r,i))
                  #shutil.move(pathToDir + f, audiobookEndPath)
                  # should be able to move top lvl folder here. But can only manage to get the folder containing the file. And also maybe stop the loop for the current top lvl dir. So I only ever want to move the first level folder (and its content) in the download directory.

    
              
              
    
                
print('files:',numberFiles , 'dirs:' , numberDir , 'total:' , totalObjects, 'epub:', isEbook, 'audiobooks', isAudio, 'audioDir:', isAudioDir)

助けてくれてありがとう。

4

0 に答える 0