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>

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

<p>is quite incomplete code. What is <code>files</code>? It probably is a list of file names, coming out of <code>os.listdir()</code>.</p>

<p>But this list lists only the filename parts (a. k. a. "basenames"), because their path is common. In order to use it correctly, you have to combine it with the path leading to it (and used to obtain it).</p>

<p>Such as (untested):</p>

<pre><code>def newest(path):
    files = os.listdir(files)
    paths = [os.path.join(path, f) for f in files]
    return max(paths, key=os.path.getctime)
</code></pre>

python