ソート対象のファイルディレクトリをカスタマイズすることで、ディレクトリ配下のすべてのファイルをファイル形式に応じて分類できます。
ロジックの実装に使用される Python テクノロジ スタックは、os、glob、shutil の 3 つの標準ライブラリを包括的に使用して、ファイルの自動並べ替えを完了します。
これら 3 つのファイル処理モジュールをそれぞれコード ブロックにインポートし、後続の開発操作を開始します。
# It imports the os module. import os # Shutil is a module that provides a number of high-level operations on files and collections of files. import shutil # The glob module finds all the pathnames matching a specified pattern according to the rules used by the Unix shell, # although results are returned in arbitrary order. No tilde expansion is done, but *, ?, and character ranges expressed # with [] will be correctly matched. import glob import sys
分類対象ファイルディレクトリのuncatched_dirと分類ファイル格納ディレクトリのtarget_dirを手動で入力できるように設定します。
# Asking the user to input the path of the directory that contains the files to be sorted. uncatched_dir = input('请输入待分类的文件路径:\n') # It checks if the uncatched_dir is empty. if uncatched_dir.strip() == '': print('待分类的文件夹路径不能为空!') sys.exit() # Asking the user to input the path of the directory that contains the files to be sorted. target_dir = input('请输入分类后文件存放的目标路径:\n') # It checks if the target_dir is empty. if target_dir.strip() == '': print('分类后的文件存放路径不能为空!') sys.exit()
入力された分類後のファイル格納ディレクトリパスが新たなパスとなる可能性があるため存在するか確認し、存在しない場合は新規パスを作成します。
# It checks if the target_dir exists. If it does not exist, it creates a new directory in the current working directory. if not os.path.exists(target_dir): # It creates a new directory in the current working directory. os.mkdir(target_dir)
ファイルの移動数を表す変数 file_move_num と、ファイルの並べ替えの結果を記録するために新しく作成されたフォルダーの数を表す変数 dir_new_num を定義します。
# A variable that is used to count the number of files that have been moved. file_move_num = 0 # A variable that is used to count the number of new directories that have been created. dir_new_num = 0
並べ替える必要があるフォルダー ディレクトリ uncatched_dir をたどり、そのディレクトリの下にあるすべての種類のファイルを自動的に並べ替えます。
# A for loop that iterates through all the files in the uncatched_dir directory. for file_ in glob.glob(f'{uncatched_dir}/**/*', recursive=True): # It checks if the file is a file. if os.path.isfile(file_): # It gets the file name of the file. file_name = os.path.basename(file_) # Checking if the file name contains a period. if '.' in file_name: # Getting the suffix of the file. suffix_name = file_name.split('.')[-1] else: # Used to classify files that do not have a suffix. suffix_name = 'others' # It checks if the directory exists. If it does not exist, it creates a new directory in the current working # directory. if not os.path.exists(f'{target_dir}/{suffix_name}'): # It creates a new directory in the current working directory. os.mkdir(f'{target_dir}/{suffix_name}') # Adding 1 to the variable dir_new_num. dir_new_num += 1 # It copies the file to the target directory. shutil.copy(file_, f'{target_dir}/{suffix_name}') # Adding 1 to the variable file_move_num. file_move_num += 1
注: フォルダー、特にシステム ディスクの移動によって発生する例外を回避するために、ここではコピー (shutil.copy 関数) が使用されます。
最後に、印刷機能を使用して、ファイル カテゴリの数と新しいフォルダーの数を印刷します。
print(f'整理完成,有{file_move_num}个文件分类到了{dir_new_num}个文件夹中!\n') input('输入任意键关闭窗口...')
プログラムの実行が完了した直後にコマンド ウィンドウが閉じられるのを避けるために、上記の input 関数を使用してウィンドウの一時停止の効果を維持します。
以上がPython に基づいてファイル分類子を実装する方法の詳細内容です。詳細については、PHP 中国語 Web サイトの他の関連記事を参照してください。

Pythonは、データサイエンス、Web開発、自動化タスクに適していますが、Cはシステムプログラミング、ゲーム開発、組み込みシステムに適しています。 Pythonは、そのシンプルさと強力なエコシステムで知られていますが、Cは高性能および基礎となる制御機能で知られています。

2時間以内にPythonの基本的なプログラミングの概念とスキルを学ぶことができます。 1.変数とデータ型、2。マスターコントロールフロー(条件付きステートメントとループ)、3。機能の定義と使用を理解する4。

Pythonは、Web開発、データサイエンス、機械学習、自動化、スクリプトの分野で広く使用されています。 1)Web開発では、DjangoおよびFlask Frameworksが開発プロセスを簡素化します。 2)データサイエンスと機械学習の分野では、Numpy、Pandas、Scikit-Learn、Tensorflowライブラリが強力なサポートを提供します。 3)自動化とスクリプトの観点から、Pythonは自動テストやシステム管理などのタスクに適しています。

2時間以内にPythonの基本を学ぶことができます。 1。変数とデータ型を学習します。2。ステートメントやループの場合などのマスター制御構造、3。関数の定義と使用を理解します。これらは、簡単なPythonプログラムの作成を開始するのに役立ちます。

10時間以内にコンピューター初心者プログラミングの基本を教える方法は?コンピューター初心者にプログラミングの知識を教えるのに10時間しかない場合、何を教えることを選びますか...

fiddlereveryversings for the-middleの測定値を使用するときに検出されないようにする方法

Python 3.6のピクルスファイルのロードレポートエラー:modulenotFounderror:nomodulenamed ...

風光明媚なスポットコメント分析におけるJieba Wordセグメンテーションの問題を解決する方法は?風光明媚なスポットコメントと分析を行っているとき、私たちはしばしばJieba Wordセグメンテーションツールを使用してテキストを処理します...


ホットAIツール

Undresser.AI Undress
リアルなヌード写真を作成する AI 搭載アプリ

AI Clothes Remover
写真から衣服を削除するオンライン AI ツール。

Undress AI Tool
脱衣画像を無料で

Clothoff.io
AI衣類リムーバー

AI Hentai Generator
AIヘンタイを無料で生成します。

人気の記事

ホットツール

AtomエディタMac版ダウンロード
最も人気のあるオープンソースエディター

MantisBT
Mantis は、製品の欠陥追跡を支援するために設計された、導入が簡単な Web ベースの欠陥追跡ツールです。 PHP、MySQL、Web サーバーが必要です。デモおよびホスティング サービスをチェックしてください。

ZendStudio 13.5.1 Mac
強力な PHP 統合開発環境

EditPlus 中国語クラック版
サイズが小さく、構文の強調表示、コード プロンプト機能はサポートされていません

SecLists
SecLists は、セキュリティ テスターの究極の相棒です。これは、セキュリティ評価中に頻繁に使用されるさまざまな種類のリストを 1 か所にまとめたものです。 SecLists は、セキュリティ テスターが必要とする可能性のあるすべてのリストを便利に提供することで、セキュリティ テストをより効率的かつ生産的にするのに役立ちます。リストの種類には、ユーザー名、パスワード、URL、ファジング ペイロード、機密データ パターン、Web シェルなどが含まれます。テスターはこのリポジトリを新しいテスト マシンにプルするだけで、必要なあらゆる種類のリストにアクセスできるようになります。
