Skip to content

os.path -> pathlib.Path #14

@TaegyunKwon

Description

@TaegyunKwon

pathlib을 쓸지에 대해 생각해 보면 좋을 거 같은데요, 저는 최근에 pathlib으로 완전 대체를 했는데,
가장 큰 이유는 folder의 파일을 iterate 할때와, 폴더 끝에 '/' 기호를 붙이고 안붙이는 것에 따라 오류가 나는 문제, 그리고 path 관련된 구문을 작성할때 깔끔하다는 장점이 있습니다.

# iterate subdirectories
# os.path
for subdir, dirs, files in os.walk(rootdir):
    for file in files:
        filepath = subdir + os.sep + file
        if filepath.endswith(".ext"):
            print(filepath)

# pathlib.Path
files = Path(rootdir).glob('**.*.ext')
for file in files:
    print(file)

# file path
# os.path
somefile = rootdir + '/' +folder + '/' + file

# pathlib.Path
somefile = Path(rootdir) / folder / file

그래서 장기적으로 + 새로작성하는 코드는 pathlib으로 작성하면 어떨까 하는데, 어제 났던 에러처럼 MusicXMLParser는 string으로 path를 받게 되어있어 충돌이 있었습니다. pathlib에 익숙하지 않은 분들이 계셔서, 포팅은 제가 하려고 하는데, pros는 결국 좋다고 cons는 cost입니다. 의견 남겨주세요

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions