2017-02-22 1 views
1

倉庫管理プロジェクトのディレクトリエディタを作成しようとしていますが、すでに作成されている新しいフォルダを作成しようとするたびに、elifブロックで指定したような問題を処理するのではなく、 :私のpythonロジックで何が問題になっていますか?

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

まず、**すべての**ディレクトリの項目をチェックする必要があります。 **最初の**は平等ではない、** 2番目の**(または他のもの)は等しいことができないからではありません。 –

答えて

0

あなたは、ディレクトリの項目を反復処理している - nameとは異なる名前のフォルダがある場合、あなたは持つフォルダもあります場合でも、if枝を入力しますその名前。ここ

最善の解決策は、私見、車輪の再発明をし、フォルダがあなたのために存在する場合のpythonチェックをさせないことです。

folder = path + name + "/" 

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

operation_chooserD() 
0

あなたは、ディレクトリ内のすべてのアイテムに新しい名前を比較しているように見えます、これは間違いなく名前に打ち勝つでしょう!= 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() 
    
    関連する問題