<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: Create subfolder and FileGeodatabase? in Python Questions</title>
    <link>https://community.esri.com/t5/python-questions/create-subfolder-and-filegeodatabase/m-p/1158760#M64194</link>
    <description>&lt;P&gt;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.&lt;/P&gt;</description>
    <pubDate>Tue, 29 Mar 2022 11:21:02 GMT</pubDate>
    <dc:creator>RPGIS</dc:creator>
    <dc:date>2022-03-29T11:21:02Z</dc:date>
    <item>
      <title>Create subfolder and FileGeodatabase?</title>
      <link>https://community.esri.com/t5/python-questions/create-subfolder-and-filegeodatabase/m-p/1158367#M64177</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;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)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Here is what I get as a print statement for the first, then second execution.&lt;span class="lia-inline-image-display-wrapper lia-image-align-inline" image-alt="First execution.png" style="width: 649px;"&gt;&lt;img src="https://community.esri.com/t5/image/serverpage/image-id/37466iC48D8E57833DCBBA/image-size/large?v=v2&amp;amp;px=999" role="button" title="First execution.png" alt="First execution.png" /&gt;&lt;/span&gt;&lt;/P&gt;&lt;P&gt;Any help on this would be greatly appreciated.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 28 Mar 2022 14:28:15 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/create-subfolder-and-filegeodatabase/m-p/1158367#M64177</guid>
      <dc:creator>RPGIS</dc:creator>
      <dc:date>2022-03-28T14:28:15Z</dc:date>
    </item>
    <item>
      <title>Re: Create subfolder and FileGeodatabase?</title>
      <link>https://community.esri.com/t5/python-questions/create-subfolder-and-filegeodatabase/m-p/1158421#M64178</link>
      <description>&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;I think you need to delete line #28 and align next if to the first one&lt;/P&gt;</description>
      <pubDate>Mon, 28 Mar 2022 15:48:01 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/create-subfolder-and-filegeodatabase/m-p/1158421#M64178</guid>
      <dc:creator>GKmieliauskas</dc:creator>
      <dc:date>2022-03-28T15:48:01Z</dc:date>
    </item>
    <item>
      <title>Re: Create subfolder and FileGeodatabase?</title>
      <link>https://community.esri.com/t5/python-questions/create-subfolder-and-filegeodatabase/m-p/1158480#M64179</link>
      <description>&lt;P&gt;I tried that at first and it keeps returning an error.&lt;/P&gt;</description>
      <pubDate>Mon, 28 Mar 2022 17:21:48 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/create-subfolder-and-filegeodatabase/m-p/1158480#M64179</guid>
      <dc:creator>RPGIS</dc:creator>
      <dc:date>2022-03-28T17:21:48Z</dc:date>
    </item>
    <item>
      <title>Re: Create subfolder and FileGeodatabase?</title>
      <link>https://community.esri.com/t5/python-questions/create-subfolder-and-filegeodatabase/m-p/1158625#M64186</link>
      <description>&lt;P&gt;I can't get past line 16 as it doesn't seem like the * passed as a path in line 35.&lt;/P&gt;&lt;P&gt;&lt;FONT color="#FF0000"&gt;The filename, directory name, or volume label syntax is incorrect: '*\\Updates'&lt;/FONT&gt;&lt;/P&gt;&lt;P&gt;Have you tried hard coding the WorkingFolder and see if you get past the error?&lt;/P&gt;&lt;P&gt;R_&lt;/P&gt;</description>
      <pubDate>Mon, 28 Mar 2022 22:35:17 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/create-subfolder-and-filegeodatabase/m-p/1158625#M64186</guid>
      <dc:creator>RhettZufelt</dc:creator>
      <dc:date>2022-03-28T22:35:17Z</dc:date>
    </item>
    <item>
      <title>Re: Create subfolder and FileGeodatabase?</title>
      <link>https://community.esri.com/t5/python-questions/create-subfolder-and-filegeodatabase/m-p/1158635#M64188</link>
      <description>&lt;P&gt;Once I hardcode the working directory, it appears as if it is not getting past this part:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;    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&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Since the "Updates" folder doesn't exist yet, nothing past the&amp;nbsp;if directory[0] in names: resolves to true.&lt;/P&gt;&lt;P&gt;So when it hits this part:&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;LI-CODE lang="python"&gt;    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)&lt;/LI-CODE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;the subfolder doesn't exist, so it creates it.&amp;nbsp; BUT, it doesn't execute the else: as the first part was true.&lt;/P&gt;&lt;P&gt;Then next time you run it, subfolder now exists, so is not None, so it executes the else: statement and creates the FGDB.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;R_&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Mon, 28 Mar 2022 23:16:14 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/create-subfolder-and-filegeodatabase/m-p/1158635#M64188</guid>
      <dc:creator>RhettZufelt</dc:creator>
      <dc:date>2022-03-28T23:16:14Z</dc:date>
    </item>
    <item>
      <title>Re: Create subfolder and FileGeodatabase?</title>
      <link>https://community.esri.com/t5/python-questions/create-subfolder-and-filegeodatabase/m-p/1158760#M64194</link>
      <description>&lt;P&gt;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.&lt;/P&gt;</description>
      <pubDate>Tue, 29 Mar 2022 11:21:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/create-subfolder-and-filegeodatabase/m-p/1158760#M64194</guid>
      <dc:creator>RPGIS</dc:creator>
      <dc:date>2022-03-29T11:21:02Z</dc:date>
    </item>
    <item>
      <title>Re: Create subfolder and FileGeodatabase?</title>
      <link>https://community.esri.com/t5/python-questions/create-subfolder-and-filegeodatabase/m-p/1158808#M64196</link>
      <description>&lt;P&gt;Try this below. I have changed WorkingFolder for testing.&lt;/P&gt;&lt;LI-CODE lang="python"&gt;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)&lt;/LI-CODE&gt;</description>
      <pubDate>Tue, 29 Mar 2022 13:06:58 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/create-subfolder-and-filegeodatabase/m-p/1158808#M64196</guid>
      <dc:creator>GKmieliauskas</dc:creator>
      <dc:date>2022-03-29T13:06:58Z</dc:date>
    </item>
    <item>
      <title>Re: Create subfolder and FileGeodatabase?</title>
      <link>https://community.esri.com/t5/python-questions/create-subfolder-and-filegeodatabase/m-p/1158843#M64197</link>
      <description>&lt;P&gt;Thank you very much &lt;a href="https://community.esri.com/t5/user/viewprofilepage/user-id/42133"&gt;@GKmieliauskas&lt;/a&gt;&amp;nbsp;,&lt;/P&gt;&lt;P&gt;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.&lt;/P&gt;&lt;P&gt;Also, I am trying to keep this section of code constant with all my scripts to eliminate the process of recreating baseline data.&amp;nbsp;I don't know if it makes sense to do this or not but just thought to ask.&lt;/P&gt;</description>
      <pubDate>Tue, 29 Mar 2022 13:53:20 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/create-subfolder-and-filegeodatabase/m-p/1158843#M64197</guid>
      <dc:creator>RPGIS</dc:creator>
      <dc:date>2022-03-29T13:53:20Z</dc:date>
    </item>
    <item>
      <title>Re: Create subfolder and FileGeodatabase?</title>
      <link>https://community.esri.com/t5/python-questions/create-subfolder-and-filegeodatabase/m-p/1158849#M64198</link>
      <description>&lt;P&gt;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.&lt;/P&gt;</description>
      <pubDate>Tue, 29 Mar 2022 14:04:46 GMT</pubDate>
      <guid>https://community.esri.com/t5/python-questions/create-subfolder-and-filegeodatabase/m-p/1158849#M64198</guid>
      <dc:creator>GKmieliauskas</dc:creator>
      <dc:date>2022-03-29T14:04:46Z</dc:date>
    </item>
  </channel>
</rss>

