programing

새 폴더 작성 방법

copyandpastes 2022. 10. 19. 21:33
반응형

새 폴더 작성 방법

내 프로그램의 출력 정보를 폴더에 저장하려고 합니다. 지정된 폴더가 없으면 프로그램에서 지정한 폴더 이름으로 새 폴더를 만들어야 합니다.이게 가능합니까?만약 그렇다면 방법을 알려주세요.

폴더 경로를 다음과 같이 지정했다고 가정합니다."C:\Program Files\alex"그리고.alex폴더가 존재하지 않습니다.프로그램에서 작성해야 합니다.alex출력 정보를 저장해야 합니다.alex폴더입니다.

os.makedirs()사용하여 폴더를 만들 수 있습니다.
os.path.display()사용하여 이미 존재하는지 확인합니다.

newpath = r'C:\Program Files\arbitrary' 
if not os.path.exists(newpath):
    os.makedirs(newpath)

인스톨러를 작성하려고 하는 경우:Windows Installer는 많은 작업을 수행합니다.

os.mkdir를 사용해 보셨습니까?

다음과 같은 코드 스니펫을 사용해 보십시오.

mypath = ...
if not os.path.isdir(mypath):
   os.makedirs(mypath)

makedirs는 필요에 따라 여러 수준의 디렉토리를 작성합니다.

필요에 따라 중간 디렉토리도 작성되므로 os.makedirs가 필요할 수 있습니다.

import os

#dir is not keyword
def makemydir(whatever):
  try:
    os.makedirs(whatever)
  except OSError:
    pass
  # let exception propagate if we just can't
  # cd into the specified directory
  os.chdir(whatever)

언급URL : https://stackoverflow.com/questions/1274405/how-to-create-new-folder

반응형