今回はフォルダ内のファイルやフォルダ名を全て取得する
pythonプログラムを作成しました。
※学習用にプログラムを作成した。
また、メモ程度にしか記載していないためご了承ください。
以下の記事を参考にさせていただきました。
使用方法
①フォルダ名やファイル名を取得したいフォルダのパスをfile.pyの
Folderpathに記載する
②'file-ダブルクリック-.bat'をダブルクリックする
※①で使用するfile.pyと同じディレクトリに格納してください。
③'list.txt'に指定したフォルダ内の構成が表示されている
※ファイルは拡張子をつけて表示させています。
①指定したフォルダ内のフォルダやファイルを取得する (file.py)
file.py
import pathlib
def main():
Folderpath = "../" #取得したいフォルダのパスを記載する
outputpath = 'list.txt'#テキストファイルを作成
f = open(outputpath, mode='w')#テキストファイルを書き込みモードで開く
GetFolderFileNames(Folderpath, 0, f)
def GetFolderFileNames(path, kaiso, f):
files = pathlib.Path(path).glob('*')#その階層のフォルダやファイルを取得
for file in files:
output = '\t' * kaiso + file.name + '\n'
f.write(output)
if file.is_file() == False:
GetFolderFileNames(file, kaiso+1, f)
if __name__ == "__main__":
main()
②file.pyの実行 (file-ダブルクリック-.bat)
file-ダブルクリック-.bat
:pythonファイルを同じディレクトリに置いてダブルクリック
@echo off
cd /d %~dp0
py file.py
pause
③フォルダ内の構成が表示されている (list.txt)
list.txt
A
AA
AA_dummy.txt
A_dummy.txt
B
B_dummy.txt
C
C_dummy.txt
D
file-ダブルクリック-.bat
file.py
list.txt
任意のフォルダ内から特定の文字列を含むフォルダ名やファイル名を検索
folder.py
import tkinter as tk
from tkinter import filedialog
import os
def browse_folder():
folder_path = filedialog.askdirectory() # フォルダ選択ダイアログを開く
folder_entry.delete(0, tk.END) # 以前のフォルダパスをクリア
folder_entry.insert(0, folder_path)
def search_files_and_folders():
search_term = search_entry.get() # ユーザー入力の検索文字列を取得
directory = folder_entry.get()
if not directory or not search_term:
result_listbox.delete(0, tk.END)
result_listbox.insert(tk.END, "フォルダと検索文字列を入力してください。")
return
result_listbox.delete(0, tk.END) # 以前の検索結果をクリア
try:
for root, dirs, files in os.walk(directory): # フォルダ内を再帰的に検索
for name in dirs + files: # フォルダ名とファイル名を検索
if search_term in name:
result_listbox.insert(tk.END, os.path.join(root, name))
except FileNotFoundError:
result_listbox.insert(tk.END, "指定されたフォルダが見つかりません。")
# GUIのセットアップ
root = tk.Tk()
root.title("フォルダ名とファイル名検索")
frame = tk.Frame(root)
frame.pack(padx=10, pady=10)
tk.Label(frame, text="フォルダ選択:").pack()
folder_entry = tk.Entry(frame, width=50)
folder_entry.pack()
browse_button = tk.Button(frame, text="フォルダ選択", command=browse_folder)
browse_button.pack()
tk.Label(frame, text="検索文字列:").pack()
search_entry = tk.Entry(frame, width=30)
search_entry.pack()
search_button = tk.Button(frame, text="検索", command=search_files_and_folders)
search_button.pack()
result_listbox = tk.Listbox(frame, width=100, height=30)
result_listbox.pack()
root.mainloop()
任意のフォルダ内のファイルパスを取得してダブルクリックでファイルを開く
test.py
import tkinter as tk
from tkinter import filedialog
import os
import webbrowser
def select_folder():
global folder_selected
folder_selected = filedialog.askdirectory()
if folder_selected:
listbox.delete(0, tk.END)
file_paths.clear()
for file_name in os.listdir(folder_selected):
full_path = os.path.join(folder_selected, file_name)
file_paths.append(full_path)
listbox.insert(tk.END, full_path)
def search_files():
query = entry_search.get().lower()
listbox.delete(0, tk.END)
for file_path in file_paths:
if query in os.path.basename(file_path).lower():
listbox.insert(tk.END, file_path)
def open_link(event):
selected_item = listbox.get(listbox.curselection())
webbrowser.open(selected_item) # ファイル名がURLの場合は開く
root = tk.Tk()
root.title("フォルダ内ファイル検索")
frame = tk.Frame(root)
frame.pack(padx=10, pady=10)
btn_select = tk.Button(frame, text="フォルダを選択", command=select_folder)
btn_select.pack()
entry_search = tk.Entry(frame, width=50)
entry_search.pack()
entry_search.bind("<KeyRelease>", lambda event: search_files()) # 入力ごとに検索
listbox = tk.Listbox(frame, width=70, height=20)
listbox.pack()
listbox.bind("<Double-Button-1>", open_link)
file_paths = [] # フォルダ内のファイルリストを格納
root.mainloop()