Folder File 탐색

Published onesixx on

https://hleecaster.com/python-pathlib/

https://onesixx.com/import/

https://onesixx.com/import-path/

os.path.join() 경로만들기

import os

root = '/Users/onesixx/my/Folder00'
root = os.path.join('/Users/onesixx', 'my', 'Folder00')

os.getcwd() 현재 위치 (cf. pwd)

os.getcwd()   # '/Users/onesixx/my/Folder00'

os.chdir() 현재위치 변경

os.chdir('/Users/onesixx/my')
os.getcwd()   #  '/Users/onesixx/my'


os.chdir('/home/onesixx/my/git/mmdetection')
myData_root = os.path.join(os.getcwd(), 'data', 'pig')

os.listdir() 현재폴더내의 folder&file

os.listdir(root)
# ['00.txt', 'ff.py', 'Folder01']

[f for f in os.listdir(root) if os.path.isdir(f)]
# ['Folder01']

[f for f in os.listdir(root) if os.path.isfile(f)]
# ['00.txt', 'ff.py']

os.walk()

for allPath, folders, files in os.walk('/Users/onesixx/my/Folder00'):
    print('-'*60)
    print(f'path:    {allPath}')
    print(f'folders: {folders}')
    print(f'files:   {files}')
# ------------------------------------------------------------
# path:    /Users/onesixx/my/Folder00
# folders: ['Folder01']
# files:   ['00.txt', 'ff.py']
# ------------------------------------------------------------
# path:    /Users/onesixx/my/Folder00/Folder01
# folders: ['Folder02']
# files:   ['01.txt']
# ------------------------------------------------------------
# path:    /Users/onesixx/my/Folder00/Folder01/Folder02
# folders: []
# files:   ['02.txt']

os.walk() : full path

for path, _, files in os.walk(root):
    for fileNm in files:
        print(os.path.join(path, fileNm))

# /Users/onesixx/my/Folder00/00.txt
# /Users/onesixx/my/Folder00/ff.py
# /Users/onesixx/my/Folder00/Folder01/01.txt
# /Users/onesixx/my/Folder00/Folder01/Folder02/02.txt

os.path.splitext() 파일의 확장자

for (path, folders, files) in os.walk(root):
    for filename in files:
        fileWithExt = os.path.splitext(filename)
        print(fileWithExt)

# ('00', '.txt')
# ('ff', '.py')
# ('01', '.txt')
# ('02', '.txt')
for (path, folders, files) in os.walk(root):
    for filename in files:
        ext = os.path.splitext(filename)[-1]
        if ext == '.py' :
            print(f'{path}/{filename}')

# /Users/onesixx/my/Folder00/ff.py           
for root, dirs, files in os.walk(log_path): 
    file_list = list(filter(lambda s: s[-3:]=="log" , files))
    file_list.sort()
    print(file_list)
    break;

# /Users/onesixx/my/Folder00/ff.py           

example

# string
root_dir = log_path
for (root, dirs, files) in os.walk(root_dir):
    print('_'*66)
    print(f'root : {root}')
    if len(dirs) > 0:
        [print(f'dir : {dir_name}') for dir_name in dirs]
    if len(files) > 0:
        [print(f'file : {file_name}') for file_name in files]
           

# list
for (root, dirs, files) in os.walk(root_dir):
    print('_'*66)
    print(f'root : {root}')
    print(f'dirs : {dirs}')
    print(f'files: {files}')  
Categories: Python Basic

onesixx

Blog Owner

Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x