2

I want to create folder and subfolder in an a specific directory by writing code in python.

It is important to check directory and folder if exist a specific subfolder, avoid duplicate or overwrite.

I create a loop but when directory or folder is empty it does not go to the loop or it gives me error.

I don't know how can create my folders.

Part of my code is like this:

# Set workspace
env.workspace = r"..\..\Test"

# Set local variables
main_folder_path = r"..\..\Test"
main_foldNam1 = "OutPutSpace"

# Execute CreateFolder
folders = os.listdir(main_folder_path)
for folder in folders:
#    if os.path.isdir(os.path.join(main_folder_path, main_foldNam1)) == True:
    if arcpy.Exists(main_foldNam1):
        print main_foldNam1, "folder is Exist", "\n"
    else:  
        arcpy.CreateFolder_management(main_folder_path, main_foldNam1)

gdb_name = "CaProduc.gdb"
sub_folder1_path = r"..\..\Test\OutPutSpace"

geoDataBs = os.listdir(sub_folder1_path)
for gdb in geoDataBs:
    if arcpy.Exists(gdb_name):
        print gdb_name, " is Exist", "\n"

    else:
        arcpy.CreateFileGDB_management(sub_folder1_path, gdb_name)
        print gdb_name, "\n"
4

What about using os.path.exists directly? Also, I would build paths using os.path.join which makes your code work everywhere. For creating one or more directories from a path, use os.makedirs... so you have everything you need :-)

Here is an example:

import os

main_folder_path = os.path.join('..', '..', 'Test')
main_foldNam1 = "OutPutSpace"
sub_folder1_path = os.path.join(main_folder_path, main_foldNam1)

gdb_name = "CaProduc.gdb"
gdb_path = os.path.join(sub_folder1_path, gdb_name)

# Create folder if it doesn't exist yet
if os.path.exists(sub_folder1_path):
    print("folder '{}' exists!".format(sub_folder1_path))
else:
    # create directory or directories until sub_folder1_path
    os.makedirs(sub_folder1_path)

if os.path.exists(gdb_path):
    print("'{}' exists in '{}'!".format(gdb_name, sub_folder1_path))
else:
    arcpy.CreateFileGDB_management(sub_folder1_path, gdb_name)
0

I don't believe arcpy handles creation of folders, but I may be wrong.

Import the os python module, and use os.path.exists(main_folder_path) to check if the folder exists. If it doesn't, then use os.makedirs(main_folder_path) to create the folder.

  • looks like andsep posted their answer as I was typing mine. – Adam Nov 21 '16 at 22:30
  • Obviously you'll need to adapt the code into your existing script. The objective here wasn't to write the code for you, rather to point you to the library and functions you can use to achieve your aim. – Adam Nov 25 '16 at 0:15
  • I solve my problem with following code: folders = os.listdir(main_folder_path) if folders == []: os.mkdir(os.path.join(main_folder_path, main_foldNam1)) os.mkdir(os.path.join(main_folder_path, main_foldNam2)) print main_foldNam1, " & ", main_foldNam2, "\n" else: if main_foldNam1 in folders: print main_foldNam1, "folder is Exist", "\n" else: os.mkdir(os.path.join(main_folder_path, main_foldNam1)) print main_foldNam1, "\n" – Ardavan Nov 25 '16 at 0:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.