반응형

지정한 디렉토리의 하위 디렉토리 목록을 구하는 파이썬 예제코드입니다.



2023. 11. 10 최초작성




import os



# 하위 디렉토리 목록을 얻고 싶은 디렉토리를 지정합니다.
current_directory = 'c:\\windows'

# current_directory에 있는 파일과 디렉토리가 모두 출력됩니다.
get_list = os.listdir(current_directory)
print(get_list)
print()

subdirectories = []
for entry in get_list:

    # 파일 또는 디렉토리에 대한 전체 경로를 얻습니다.
    full_path = os.path.join(current_directory, entry)
   
    # 전체 경로로 디렉토리인지 검사해야 합니다.
    if os.path.isdir(full_path):

        # 디렉토리만 리스트에 추가합니다.
        subdirectories.append(full_path)


// 하위 디렉토리 목록이 출력됩니다.
print(subdirectories)



실행 결과

 

['addins', 'AhnInst.log', 'appcompat', 'apppatch', 'AppReadiness', 'assembly', 'bcastdvr', 'bfsvc.exe', 'BitLockerDiscoveryVolumeContents', 'Boot', 'bootstat.dat', 'Branding', 'BrowserCore', 'CbsTemp', 'comsetup.log', 'Containers', 'CSC', 'Cursors', 'debug', 'diagerr.xml', 'diagnostics', 'DiagTrack', 'diagwrn.xml', 'DigitalLocker', 'Downloaded Program Files', 'DPINST.LOG', 'DtcInstall.log', 'ELAMBKUP', 'en-US', 'explorer.exe', 'Fonts', 'GameBarPresenceWriter', 'Globalization', 'Help', 'HelpPane.exe', 'hh.exe', 'hipiw.dll', 'IdentityCRL', 'IME', 'ImmersiveControlPanel', 'InboxApps', 'INF', 'InputMethod', 'Installer', 'ko-KR', 'L2Schemas', 'LanguageOverlayCache', 'libem.INI', 'LiveKernelReports', 'Logs', 'Media', 'mib.bin', 'Microsoft.NET', 'Migration', 'ModemLogs', 'notepad.exe', 'NvContainerRecovery.bat', 'OCR', 'Offline Web Pages', 'Panther', 'Performance', 'PFRO.log', 'PLA', 'PolicyDefinitions', 'Prefetch', 'PrintDialog', 'Professional.xml', 'Provisioning', 'regedit.exe', 'Registration', 'RemotePackages', 'rescache', 'Resources', 'SchCache', 'schemas', 'security', 'ServiceProfiles', 'ServiceState', 'servicing', 'Setup', 'setupact.log', 'setuperr.log', 'ShellComponents', 'ShellExperiences', 'ShellNew', 'SKB', 'SoftwareDistribution', 'Speech', 'Speech_OneCore', 'splwow64.exe', 'System', 'system.ini', 'System32', 'SystemApps', 'SystemResources', 'SystemTemp', 'SysWOW64', 'TAPI', 'Tasks', 'Temp', 'TempInst', 'TextInput', 'tracing', 'twain_32', 'twain_32.dll', 'uninstallkdf8.exe', 'UUS', 'Vss', 'WaaS', 'Web', 'win.ini', 'WindowsShell.Manifest', 'WindowsUpdate.log', 'winhlp32.exe', 'WinSxS', 'WMSysPr9.prx', 'write.exe', 'WUModels']

['c:\\windows\\addins', 'c:\\windows\\appcompat', 'c:\\windows\\apppatch', 'c:\\windows\\AppReadiness', 'c:\\windows\\assembly', 'c:\\windows\\bcastdvr', 'c:\\windows\\BitLockerDiscoveryVolumeContents', 'c:\\windows\\Boot', 'c:\\windows\\Branding', 'c:\\windows\\BrowserCore', 'c:\\windows\\CbsTemp', 'c:\\windows\\Containers', 'c:\\windows\\CSC', 'c:\\windows\\Cursors', 'c:\\windows\\debug', 'c:\\windows\\diagnostics', 'c:\\windows\\DiagTrack', 'c:\\windows\\DigitalLocker', 'c:\\windows\\Downloaded Program Files', 'c:\\windows\\ELAMBKUP', 'c:\\windows\\en-US', 'c:\\windows\\Fonts', 'c:\\windows\\GameBarPresenceWriter', 'c:\\windows\\Globalization', 'c:\\windows\\Help', 'c:\\windows\\IdentityCRL', 'c:\\windows\\IME', 'c:\\windows\\ImmersiveControlPanel', 'c:\\windows\\InboxApps', 'c:\\windows\\INF', 'c:\\windows\\InputMethod', 'c:\\windows\\Installer', 'c:\\windows\\ko-KR', 'c:\\windows\\L2Schemas', 'c:\\windows\\LanguageOverlayCache', 'c:\\windows\\LiveKernelReports', 'c:\\windows\\Logs', 'c:\\windows\\Media', 'c:\\windows\\Microsoft.NET', 'c:\\windows\\Migration', 'c:\\windows\\ModemLogs', 'c:\\windows\\OCR', 'c:\\windows\\Offline Web Pages', 'c:\\windows\\Panther', 'c:\\windows\\Performance', 'c:\\windows\\PLA', 'c:\\windows\\PolicyDefinitions', 'c:\\windows\\Prefetch', 'c:\\windows\\PrintDialog', 'c:\\windows\\Provisioning', 'c:\\windows\\Registration', 'c:\\windows\\RemotePackages', 'c:\\windows\\rescache', 'c:\\windows\\Resources', 'c:\\windows\\SchCache', 'c:\\windows\\schemas', 'c:\\windows\\security', 'c:\\windows\\ServiceProfiles', 'c:\\windows\\ServiceState', 'c:\\windows\\servicing', 'c:\\windows\\Setup', 'c:\\windows\\ShellComponents', 'c:\\windows\\ShellExperiences', 'c:\\windows\\ShellNew', 'c:\\windows\\SKB', 'c:\\windows\\SoftwareDistribution', 'c:\\windows\\Speech', 'c:\\windows\\Speech_OneCore', 'c:\\windows\\System', 'c:\\windows\\System32', 'c:\\windows\\SystemApps', 'c:\\windows\\SystemResources', 'c:\\windows\\SystemTemp', 'c:\\windows\\SysWOW64', 'c:\\windows\\TAPI', 'c:\\windows\\Tasks', 'c:\\windows\\Temp', 'c:\\windows\\TempInst', 'c:\\windows\\TextInput', 'c:\\windows\\tracing', 'c:\\windows\\twain_32', 'c:\\windows\\UUS', 'c:\\windows\\Vss', 'c:\\windows\\WaaS', 'c:\\windows\\Web', 'c:\\windows\\WinSxS', 'c:\\windows\\WUModels']




반응형

문제 발생시 지나치지 마시고 댓글 남겨주시면 가능한 빨리 답장드립니다.

도움이 되셨다면 토스아이디로 후원해주세요.
https://toss.me/momo2024


제가 쓴 책도 한번 검토해보세요 ^^

+ Recent posts