get the name of most recent file in linux - PYTHON
<p>I want to get the name of the most recent file from a particular directory in python?</p>

<p>I used this</p>

<pre><code>import os
import glob

def get_latest_file(path, *paths):
    """Returns the name of the latest (most recent) file
    of the joined path(s)"""
    fullpath = os.path.join(path, *paths)
    print fullpath
    list_of_files = glob.glob(fullpath)  
    if not list_of_files:               
        return None                     
    latest_file = max(list_of_files, key=os.path.getctime)
    _, filename = os.path.split(latest_file)
    return filename


if __name__ == "__main__":
    print get_latest_file('ocr', 'uploads', '*.png')
</code></pre>

<p><a href="http://codereview.stackexchange.com/questions/120494/finding-the-latest-file-in-a-folder">Source</a> </p>

<p>But I want the code to return name of the most recent file without specifying extension of file. 
So let's say if there are jpg, jpeg, png, gif</p>

<p>I want the snippet to cover them all.</p>

<p>Any inputs?</p>

<p>with your last line you just retrieve the files with .png extension</p>

<pre><code>get_latest_file('ocr', 'uploads', '*.png')
</code></pre>

<p>if you want to retrieve all files not depending on the extension, you just need to remove your extension specification in code to glob.glob('<em>'). This will retrieve all files in your directory. if you still need any extension but it doesnt matter which you also could retrieve them with glob.glob(</em>.*) i think.</p>

python