Find all extensions in a directory and order them alphabetically
<p>I've got a homework which I am unable to solve :(</p>

<p>I have to use <code>os</code> and <code>os.path</code>.</p>

<ol>
<li><p>I have to write a program which asks you for a directory but if you don't input anything the directory is current working directory, then it prints all file's extensions ordered alphabetically.</p>

<p>The program should work like that:</p>

<p>Directory:  <code>/Users/Andrew/Desktop</code></p>

<pre><code>.ai
.docx
.jpg
.png
.py
.rar
.zip
</code></pre></li>
<li><p>I have to write a program which again asks you for a directory but if you don't input anything the directory is current working directory, then it prints all file's extensions ordered alphabetically but this time it should draw a line for every filetype and list the files with appropriate extension.</p>

<p>Directory:  <code>/Users/Andrew/Desktop</code></p>

<pre><code>.ai
----------
cat.ai

.docx
----------
Job application.docx

.png
----------
Screen Shot1.png
Untitled-1.png

.py
----------
1.py
2.py
4.py
5.py
6.py

.xls
----------
PlanB.xls
Tommorow.xls
</code></pre></li>
</ol>

<p>Your help would be appreciated.</p>

<p>This is my attempt at solving first problem:</p>

<pre><code>import os

directory=input("Directory:")

if directory:
    files=os.listdir(directory)
else:
    files=os.listdir(os.getcwd())


for filetypes in files:
    print(os.path.splitext(filetypes)[1])
</code></pre>

<p>I finally did the first assignment, but I can't solve the second one. I'd really need help.</p>

<p>Thank you</p>
