본문 바로가기
카테고리 없음

디렉토리 Search-02

by Ao1 2022. 7. 25.

import os
from tkinter import *
import tkinter.ttk as ttk

tk = Tk()
tk.geometry("500x500")

path = os.getcwd()
combobox = ttk.Combobox(tk)

'''
filename = []
filedir = []
full_filename = []
full_filedir = []
name_0 = []
ext_0 = []
t = 0


'파일 명'
with os.scandir(path) as entries:
    for entry in entries:
       if entry.is_file():
           filename.append(entry.name)

'파일 디렉토리'
with os.scandir(path) as entries:
    for entry in entries:
       if entry.is_dir():
           filedir.append(entry.name)

'경로+파일명'
for t in range(0,len(filename)):
    full_filename.append(path + '\\'+ filename[t])

'경로+디렉토리'
for t in range(0,len(filedir)):
   full_filedir.append(path + '\\'+ filedir[t])

'확장자 분리'
for t in range(0,len(filename)):
   name, ext = os.path.splitext(filename[t])
   name_0.append(name)
   ext_0.append(ext)
'''

'콤보박스 생성'
def ComBoBox():
    a=["../"]
    chdir_ck = os.getcwd()
    
    with os.scandir(chdir_ck) as entries:
        for entry in entries:
            if entry.is_dir():
                a.append("[Directory]  "+entry.name)
    with os.scandir(chdir_ck) as entries:
        for entry in entries:
            if entry.is_file():
                a.append("[File]  "+entry.name)
    combobox.config(height=10,values=a,state="readonly")
    combobox.pack()

'선택 이벤트'
def btnpress():
    '콤보 박스에서 선택한 값 반환'
    chd = " "
    b = []
    b.append(combobox.get())
    c = str(b)[3:12]
    d = str(b)[3:7]
    '상위 디렉토리 조회'
    if b in [['../']]:
        chdir_ck = os.getcwd()
        pr = chdir_ck.rindex('\\')
        chd = chdir_ck.strip()[:pr]
        os.chdir(chd)
        
        if chd in ['C:']:
            chd = "C:\\"
            os.chdir(chd)
        elif chd in ['D:']:
            chd = "D:\\"
            os.chdir(chd)
            
        ComBoBox()
            
        '디렉토리 클릭 시 이동 및 조회(다중 변경)'
    elif c in ['Directory']:
        chd = str(b)[15:-2]
        
        '콤보박스 재생성'
        os.chdir(chd)
        ComBoBox()
        
        
        '파일 클릭 시 해당 파일 선택(단일 변경)'
    elif d in ['File']:
        f_path = str(b)[10:-2]
        abs_path = os.path.abspath(f_path)
        
        '콤보박스 재생성'
        ComBoBox()


'현재 디렉토리를 받음 -> for문으로 뒤에서부터 값을 잘라냄 ->'
'if로 마지막 \ 와 만나면 해당 디렉토리 이동 -> 다시 긁기'


ComBoBox()

btn = Button(tk)
btn.config(text="선택",command=btnpress)
btn.pack()

lb = Label(tk)
lb.pack()

tk.mainloop()