Create subfolder and FileGeodatabase?

1289
8
Jump to solution
03-28-2022 07:28 AM
RPGIS
by
Occasional Contributor III

Hi,

 

I have been trying to figure out why I keep running into this strange issue. I am not sure if I programmed the script incorrectly or if it happens to be an issue with establishing the subfolder first. Here is what I have so far.

 

 

import arcpy
import os

def CreateBaseData(location, names):
    subfolder_name = None
    database_name = None
    subfolder = None
    database = None

    for name in names:
        if '.gdb' in name:
            database_name = name
        else:
            subfolder_name = name

    for root, directory, filenames in os.walk(location):
        if directory:
            if directory[0] in names:
                if database_name == directory[0]:
                    database = os.path.join(root, directory[0])
                elif subfolder_name == directory[0]:
                    subfolder = os.path.join(root, directory[0])
                else:
                    pass
    
    if subfolder is None:
        subfolder = os.mkdir(os.path.join(location, subfolder_name))
    else:
        if database is None:
            database = arcpy.CreateFileGDB_management(subfolder, database_name)
            
    return database

# Create folder and file geodatabase
WorkingFolder = r'*'

subfolderName = 'Updates'
databaseName = 'Updates.gdb'
names = [subfolderName, databaseName]

database = CreateBaseData(WorkingFolder, names)
print (database)

 

 

 

Here is what I get as a print statement for the first, then second execution.First execution.png

Any help on this would be greatly appreciated.

 

0 Kudos
1 Solution

Accepted Solutions
GKmieliauskas
Esri Regular Contributor

Try this below. I have changed WorkingFolder for testing.

import arcpy
import os


def CreateBaseData(location, names):
    subfolder_name = None
    database_name = None
    subfolder = None
    database = None

    for name in names:
        if '.gdb' in name:
            database_name = name
        else:
            subfolder_name = name

    subfolder = os.path.join(location, subfolder_name)
    database = os.path.join(subfolder, database_name)
    if (os.path.isdir(subfolder) == False):
        os.mkdir(subfolder)
        arcpy.CreateFileGDB_management(subfolder, database_name)
    else:
        if (arcpy.Exists(database) == False):
            arcpy.CreateFileGDB_management(subfolder, database_name)

    return database


# Create folder and file geodatabase
WorkingFolder = os.path.dirname(__file__)

subfolderName = 'Updates'
databaseName = 'Updates.gdb'
names = [subfolderName, databaseName]

database = CreateBaseData(WorkingFolder, names)
print(database)

View solution in original post

0 Kudos
8 Replies
GKmieliauskas
Esri Regular Contributor

Hi,

I think you need to delete line #28 and align next if to the first one

0 Kudos
RPGIS
by
Occasional Contributor III

I tried that at first and it keeps returning an error.

0 Kudos
RhettZufelt
MVP Frequent Contributor

I can't get past line 16 as it doesn't seem like the * passed as a path in line 35.

The filename, directory name, or volume label syntax is incorrect: '*\\Updates'

Have you tried hard coding the WorkingFolder and see if you get past the error?

R_

0 Kudos
RhettZufelt
MVP Frequent Contributor

Once I hardcode the working directory, it appears as if it is not getting past this part:

 

    for root, directory, filenames in os.walk(location):
        if directory:
            if directory[0] in names:
                if database_name == directory[0]:
                    database = os.path.join(root, directory[0])
                elif subfolder_name == directory[0]:
                    subfolder = os.path.join(root, directory[0])
                else:
                    pass

 

 

Since the "Updates" folder doesn't exist yet, nothing past the if directory[0] in names: resolves to true.

So when it hits this part:

 

    if subfolder is None:
        subfolder = os.mkdir(os.path.join(location, subfolder_name))
    else:
        if database is None:
            database = arcpy.CreateFileGDB_management(subfolder, database_name)

 

 

the subfolder doesn't exist, so it creates it.  BUT, it doesn't execute the else: as the first part was true.

Then next time you run it, subfolder now exists, so is not None, so it executes the else: statement and creates the FGDB.

 

Think you need to deal with checking/creating the working folder before you walk through it. Or, just have it check if subfolder exists and database doesn't, create it.

R_

 

0 Kudos
RPGIS
by
Occasional Contributor III

Would it be recommended to have to loop through again to look for the database or have it simply check to see if either folder exists? I thought about using os.path.exists to determine the existence of both the database and folder. Depending on which exists; create either one or the other.

0 Kudos
GKmieliauskas
Esri Regular Contributor

Try this below. I have changed WorkingFolder for testing.

import arcpy
import os


def CreateBaseData(location, names):
    subfolder_name = None
    database_name = None
    subfolder = None
    database = None

    for name in names:
        if '.gdb' in name:
            database_name = name
        else:
            subfolder_name = name

    subfolder = os.path.join(location, subfolder_name)
    database = os.path.join(subfolder, database_name)
    if (os.path.isdir(subfolder) == False):
        os.mkdir(subfolder)
        arcpy.CreateFileGDB_management(subfolder, database_name)
    else:
        if (arcpy.Exists(database) == False):
            arcpy.CreateFileGDB_management(subfolder, database_name)

    return database


# Create folder and file geodatabase
WorkingFolder = os.path.dirname(__file__)

subfolderName = 'Updates'
databaseName = 'Updates.gdb'
names = [subfolderName, databaseName]

database = CreateBaseData(WorkingFolder, names)
print(database)
0 Kudos
RPGIS
by
Occasional Contributor III

Thank you very much @GKmieliauskas ,

That seems to do the trick. I don't know why I couldn't figure out the little tidbit that kept holding me up. I am trying to simplify my scripts, using functions, to reduce the amount of code I have to write.

Also, I am trying to keep this section of code constant with all my scripts to eliminate the process of recreating baseline data. I don't know if it makes sense to do this or not but just thought to ask.

0 Kudos
GKmieliauskas
Esri Regular Contributor

Less code is right way. It is better to use arcpy functionality to check database existing or where available. Make python modules to store reusable code.

0 Kudos