How to get the latest file in a folder using python
<p>i need to get the latest file of a folder using python. While using the code:</p>

<pre><code>max(files, key = os.path.getctime)
</code></pre>

<p>I am getting the below error:  </p>

<blockquote>
  <p>FileNotFoundError: [WinError 2] The system cannot find the file specified: 'a'</p>
</blockquote>

<p>Try to sort items by creation time. Example below sorts files in a folder and gets first element which is latest.</p>

<pre><code>import glob
import os

files_path = os.path.join(folder, '*')
files = sorted(
    glob.iglob(files_path), key=os.path.getctime, reverse=True) 
print files[0]
</code></pre>

python