λ³Έλ¬Έ λ°”λ‘œκ°€κΈ°
IT-Engineering/Python

파이썬(Python) 디렉토리 생성, μ—†μœΌλ©΄ λ§Œλ“€κΈ°

by 🧞‍♂️ 2020. 10. 20.
λ°˜μ‘ν˜•

파이썬(Python)으둜 디렉토리(directory)λ₯Ό 생성할 λ•Œ λ°”λ‘œ λ§Œλ“€ μˆ˜λ„ μžˆμ§€λ§Œ, μ—†λŠ”μ§€ 체크λ₯Ό ν•˜κ³  λ§Œλ“€λ©΄ μ•ˆμ „ν•˜κ²Œ λ§Œλ“€ μˆ˜λ„ 있고, ν•¨μˆ˜λ₯Ό ν•˜λ‚˜ 잘 λ§Œλ“€μ–΄ 두면 디렉토리가 μžˆλ“  μ—†λ“  ν•¨μˆ˜ ν˜ΈμΆœμ„ ν•˜λ©΄ λœλ‹€λŠ” μž₯점이 μžˆλ‹€.

κ±°λ‘μ ˆλ―Έν•˜κ³  λ°”λ‘œ μ•„λž˜μ˜ ν•¨μˆ˜λ₯Ό κ°€μ Έλ‹€κ°€ μ‚¬μš©ν•˜λ©΄ λœλ‹€. 직접 λ§Œλ“  ν•¨μˆ˜μ΄λ‹ˆ 도움이 되기λ₯Ό λ°”λž€λ‹€.

import os

def createDirectory(directory):
    try:
        if not os.path.exists(directory):
            os.makedirs(directory)
    except OSError:
        print("Error: Failed to create the directory.")

try, except ꡬ문을 톡해 ν•œμΈ΅ 더 μ•ˆμ „ν•˜κ²Œ λ§Œλ“€μ–΄μ§„ ν•¨μˆ˜μ΄λ‹ˆ 막 κ°€μ Έλ‹€κ°€ createDirectory()ν•¨μˆ˜λ₯Ό ν˜ΈμΆœν•΄μ„œ μ“°μž!

μ›ν•˜λŠ” ν΄λ”λ‚˜ 디렉토리가 잘 μƒμ„±λ˜κ³  μžˆμ„ 것이닀.

λ°˜μ‘ν˜•

λŒ“κΈ€