2017-02-22 1 views
1

프로젝트를 관리하는 창고에 대한 디렉토리 편집기를 만들려고 시도하고 있지만, 이미 작성된 새 폴더를 만들려고 할 때마다 elif 블록에서 지정한 것과 같은 문제를 처리하는 대신이 오류가 발생합니다 :파이썬 논리가 잘못되었습니다.

FileExistsError: [WinError 183] Cannot create a file when that file already exists: 'C:/Users/User_Name/Documents/Warehouse_Storage/folder_name' 

내가 알 수있는 한, if 문의 기본 논리에는 아무런 문제가 없습니다. 여기

내 코드입니다 :

if operation.lower() == "addf" : 

    name = input("What would you like to name your new folder? \n") 

    for c in directory_items : 
     if name != c : 
      os.makedirs(path + name + "/") 
      operation_chooserD() 

     elif name == c: 
      print("You already created a folder with this name.") 
      operation_chooserD() 
+2

먼저 ** 모든 ** 디렉토리 _ 항목을 확인해야합니다. ** 첫 번째 **가 평등하지 않기 때문에, ** 두 번째 ** (또는 다른 모든 것)가 동일하지 않을 수는 없습니다. –

답변

0

당신은 디렉토리 항목을 반복하고 - 폴더가 name가 아닌 다른 이름이 있다면 또한있는 폴더이 경우에도, 당신의 if 분기를 입력합니다 그 이름.

folder = path + name + "/" 

if os.path.exists(folder): 
    print("You already created a folder with this name.") 
else: 
    os.makedirs(folder) 

operation_chooserD() 
0

당신은 디렉토리에있는 모든 항목에 새 이름을 비교하는 것 같다 : 폴더가 당신을 위해 존재하는 경우

여기에 가장 좋은 해결책, IMHO는 바퀴를 재발견하고, 파이썬 검사를 못하게하는 것입니다 , 이것은 분명히 이름에 부딪 칠 것입니다! = c 조건 (여러 번). 이 경우 루프는 불필요합니다.

너는의 라인을 따라 뭔가를 시도 할 수 있습니다.

if name in c: 
//do stuff if name exists 
else: 
//create the directory 
1

당신의 논리에 문제가 몇 가지 있습니다 :/ELIF 테스트가 중복 인 경우 디렉토리

  • 마다 항목을 새 항목을 만들려고

    정말로하고 싶은 내용은 다음과 같습니다.

    if c not in directory_items: 
        os.makedirs(path + name + "/") 
        operation_chooserD() 
    
    else: 
        print("You already created a folder with this name.") 
        operation_chooserD() 
    
  • 0

    아마도 directory_items는 현재 디렉토리의 파일 이름 목록입니다.

    if operation.lower() == "addf" : 
    
        name = input("What would you like to name your new folder? \n") 
        print(directory_items) 
        # check here what you are getting in this list, means directory with/or not. If you are getting 
        if name not in directory_items: 
         os.makedirs(path + name + "/") 
         operation_chooserD() 
        else: 
         print("You already created a folder with this name.") 
         operation_chooserD() 
    
    관련 문제