Exit Python program if data directory is empty
<p>UPDATE: I think I may have just realized what I need to figure out re: the below, which is the correct error type to specify with the except clause (new to this, obviously)</p>

<p>Starting in a specified root directory, my program iterates through subdirectories, then files within those, identifying valid (.csv) data files and then aggregating and performing calculations on the data.  </p>

<p>In cases where the root directory happens to be empty, can someone suggest a clean/graceful way to simply exit the program at the start without further processing?  </p>

<p>I tried to adapt a suggestion I found here, but it didn't work as I expected:
<a href="https://stackoverflow.com/questions/13424926/exit-gracefully-if-file-doesnt-exist">Exit gracefully if file doesn&#39;t exist</a></p>

<p>That person's code was:</p>

<pre><code>def main():
    try:
        file = open('file.txt', 'r')
    except IOError:
        print('There was an error opening the file!')
        return
</code></pre>

<p>I gave the above a try, and it works for the particular case above. However, when I tried to adapt it as follows, it 'broke' and I got an "Index out of range error", instead dropping down to the except code. </p>

<pre><code>dir = os.listdir(masterDirPath)
def main():
    try:
        item = dir[0]
    except IOError:
        print('The data area is empty.')
        return
</code></pre>

<p>(Also/instead, I very much welcome suggestions for some completely other approach to the task overall)</p>