彼女のパソコンのデスクトップはこんな感じ。
幸いマックとしか言いようがない。そうしないと、コンピューターが非常に動かなくなるはずです。今日は自分の意見を整理する方法も教えます。
さまざまなファイル拡張子がさまざまなカテゴリに分類されます
まず、いくつかの種類のファイルを大まかにリストし、ファイルのサフィックスに従って設定します。大まかに次のようになります。
SUBDIR = { "ドキュメント": [".pdf"、".docx"、".txt"、".html"]、" オーディオ": [".m4a"、".m4b"、".mp3"、" .mp4"]、 "画像": [".jpg"、".jpeg"、".png"、".gif"]、 "データファイル": [".csv"、".xlsx"] }
上記のファイル接尾辞は包括的なものではありません. 読者は自分の必要に応じてそれらを追加し, 好みに応じて分類することができます. 次に, 入力ファイル接尾辞に基づいているかどうかを判断する関数をカスタマイズします. どのカテゴリに属しますか?
def pickDir ( value ):
for category 、extent in SUBDIR . items ( ) :
for suffix in extent :
if suffix == value :
return category
たとえば、入力が返される場合は、このクラス.pdf
です。DOCUMENTS
また、現在のディレクトリ内のすべてのファイルをトラバースし、多くのファイルのサフィックスを取得し、異なるサフィックスを持つこれらのファイルを異なるカテゴリのフォルダーに移動する関数をカスタマイズする必要があります. コードは次のとおりです.
def OrganizeDir(path_val):
for item in os.scandir(path_val):
if item.is_dir():
続行
filePath = Path(item)
file_suffix = filePath.suffix.lower()
directory = pickDir(file_suffix)
directoryPath = Path(directory )
# フォルダーが存在しない場合は、新しいフォルダーを作成する
if directoryPath.is_dir() != True:
directoryPath.mkdir()
filePath.rename(directoryPath.joinpath(filePath))
出力
もう一度ベースでPython
ビルドしてから、カプセル化しましょう可视化GUI界面
. コードは次のとおりです
class FileOrgnizer(QWidget):
def __init__(self):
super().__init__()
self.lb = QLabel(self)
self.lb.setGeometry(70, 25, 80, 40)
self.lb.setText('文件夹整理助手:')
self.textbox = QLineEdit(self)
self.textbox.setGeometry(170, 30, 130, 30)
self.findButton = QPushButton('整理', self)
self.findButton.setGeometry(60, 85, 100 , 40)
self.quitButton = QPushButton('退出', self)
self.quitButton.clicked.connect(self.closeEvent)
self.findButton.clicked.connect(self.organizeDir)
self.quitButton.setGeometry(190, 85, 100 , 40)
self.setGeometry(500, 500, 350, 150)
self.setWindowTitle('Icon')
self.setWindowIcon(QIcon('../751.png'))
self.show()
def pickDir(self, value):
カテゴリの場合、SUBDIR.items() の ekstensi:
サフィックスの場合in ekstensi:
if suffix == value:
return category
def OrganizeDir(self, event):
path_val = self.textbox.text()
print("経路径以: " + path_val)
for item in os.scandir(path_val):
if item .is_dir():
続行
filePath = Path(item)
fileType = filePath.suffix.lower()
directory = self.pickDir(fileType)
if directory == None:
続行
directoryPath = Path(directory)
if directoryPath.is_dir() != True:
directoryPath.mkdir()
filePath.rename(directoryPath.joinpath(filePath))
reply = QMessageBox.information(self, "Done" , "タスクが完了しました。終了しますか?", QMessageBox.Yes | QMessageBox.No, QMessageBox.No)
if reply == QMessageBox.Yes:
event.accept()
else:
event.ignore()
def closeEvent( self, event ):
reply = QMessageBox.question(self, 'Exit',
"終了してもよろしいですか?", QMessageBox.Yes |
QMessageBox.No, QMessageBox.No)
if reply == QMessageBox.Yes:
event.accept()
else:
event.ignore()
以下に示す結果
最後に、pyinstaller
モジュールを介してPython
コードを実行可能ファイルにパッケージ化します。操作手順は次のとおりです。
pyinstaller -F -w 文件名.py
一部のパラメータの意味は次のとおりです。
-
-F
: 単一の実行可能ファイルを生成することを意味します -
-w
GUI
:インターフェイスで非常に便利なコンソール ウィンドウを削除することを示します。 -
-i
:実行ファイルを表すアイコン
公開アカウントに注意してください: 完全なコードを受け取る Python Gu Muzi