Pythonを使った自動ファイルオーガナイザ


私は、私がunsortedされたファイルのトンでフォルダの1ダースを持っているただ一人の人でないと確信します.映画、書籍、音楽、同じフォルダ内のすべてのドキュメント.私たちは何かをダウンロードしてください私たちはそれを知っている前に我々のダウンロードフォルダーは混乱です.Y ' allこれを認識👇?

あなたがするならば、あなたに恥を!どのように、あなたはこれを起こることができますか.とにかく、時間を無駄にしないでください.
ファイルを整理するとき、私はOCDを持っています.私は俳優、リリース年、ジャンルなどの私の映画を並べ替えますが、それはあなたが非常に多くのファイルを持っているときに、ファイルをチェックしてソートするために困難で面倒なタスクでありえます.なぜ、私たちが私たちのためにそれを気にかけることができるとき、すべてのフォルダを通過して、ファイル・タイプによって各々のファイルを分類している我々の時間を浪費するのを悩ます理由.これはあなたが望むすべてをしないかもしれません、しかし、それは本当に良い第一歩です.
それ以上のADOなしで、コードを得ましょう
私たちが最初にすることはos and shutil モジュール

The shutil module offers a number of high-level operations on files and collections of files. In particular, functions are provided which support file copying and removal.


我々が使うことができる事実と結合した os モジュールは、パスに関連する問題を管理するために、我々は我々が望むフォルダにファイルを移動するの全プロセスを自動化することができます.
では、必要に応じて新しいディレクトリを移動して作成するためのロジックを含む関数を書きましょう.
def Organizer(path, userSelection):

    if os.path.exists(path):
        for file in os.listdir(path):

            filePath = os.path.join(path, file)

            if os.path.isdir(filePath):
                pass

            filename, fileExtension = os.path.splitext(filePath)

            if fileExtension in userSelection:

                relocatingPath = os.path.join(
                    path, userSelection[fileExtension])

                if not os.path.exists(relocatingPath):
                    os.makedirs(relocatingPath)

                try:
                    shutil.move(filePath, relocatingPath)
                except OSError as error:
                    print('''Something unexpected happened! 
                        Error message:''', error)
        print("\n\tYour files are sorted Successfully!")
ご覧のように、この関数には2つのパラメータがあります.path , userSelection . 我々は、両方の瞬間を定義されます.
私たちがどのようにファイルを扱うためにロジックを実装するつもりであるかについて、ちょっとして話しましょう.
定義済みの辞書を持っていますextenstionSet 最も一般的なファイルタイプ.

The dictionary will have file extensions(i.e. '.py', '.exe', '.mp4' ...) as keys and folder names(i.e. Movies, Documents ...) as values.


extenstionSet = {
        ".docx": "Documents",
        ".xslx": "Documents",
        ".pdf": "Documents",
        ".csv": "Documents",
        ".xlsx": "Documents",
        ".zip": "Compressed",
        ".rar": "Compressed",
        ".mp3": "Musics",
        ".m4a": "Musics",
        ".ogg": "Musics",
        ".wav": "Musics",
        ".jpg": "Pictures",
        ".png": "Pictures",
        ".gif": "Pictures",
        ".tif": "Pictures",
        ".mp4": "Videos",
        ".mkv": "Videos",
        ".3gp": "Videos",
        ".mpeg4": "Videos",
    }
では、ユーザからの入力としてフォルダのパスを取るコードを書きましょう.
path = input('''\tPaste the path you want to organize 
    >>> ''')

while not os.path.exists(path):
    print('''\n\tThe path you entered doesn\'t exist. 
    Make sure there aren\'t spelling errors
    and enter the correct path. ''')

    path = input('''\n\tPaste the path you want to organize 
    >>> ''')
ユーザに二つのオプションを提供します.
  • 定義済みディレクトリを使用してディレクトリ内のすべてのファイルをソートするextensionSet 辞書
  • ファイル拡張子のカスタムセットを入力して、特定のファイルを並べ替えます.以来extensionSet 拡張子数が限られている場合、このオプションは拡張子を持つファイルを並べ替えることができます.
  • これを行うには、ユーザーが選択した拡張子を格納し、ユーザーが上記の2つのオプションから選択できるようにするメニューを作成する空の辞書を作成します.
    userSelection = {}
    userMenu = 0
    
    while userMenu == 0:
        userMenu = input('''
            In what way do you want to organize.
    
            1). Organize all files in the directory.
            2). Organize files of certain extensions in the directory.
    
            >>> ''')
    
        if userMenu == '1':
            userSelection = extenstionSet
    
        elif userMenu == '2':
            ext = input('''\n\tEnter the extensions you want to organize
            preceeded by a period and separated by space.
            For-example:- '.py .json .csv' 
            >>> ''')
    
            folderName = input('''\n\tEnter a folder name of your choice
            for the selected extensions 
            >>> ''')
    
            for i in ext.split():
                userSelection[i] = folderName
        else:
            print(f'''\n\tYou have entered an invalid input of {userMenu}
        Please enter a valid input from the options provided. \n''')
            userMenu = 0
    
    今ではすべてが残っているすべてのソートされている私たちの関数を呼び出します.
    if __name__ == "__main__":
        Organizer(path, userSelection)
    
    ここでスクリプトを見つけることができます.https://github.com/nabroleonx/Automated-file-organizer
    あなたがそれを好むならば😉
    P . S .私がしたことは基本的な実装です.しかし、スクリプトに新しい機能を追加します.ステイ!
    今日のブログです.最後に貼り付けてくれてありがとう.私はnoobの作家は私のゲームを取得しようとしているので、任意のコメント/提案が評価されます.私はもっと多くの有益なコンテンツを投稿したいと思っています.
    陳川Github