How to check which file is the latest in a Python folder?

Asked

Viewed 3,036 times

7

Suppose you have a folder with several files .txt, with different names, I need to know the latest file.

How do I do this using Python?

2 answers

13


You can use the module pathlib.

from pathlib import Path

data_criacao = lambda f: f.stat().st_ctime
data_modificacao = lambda f: f.stat().st_mtime

directory = Path('/seu/diretorio')
files = directory.glob('*.txt')
sorted_files = sorted(files, key=data_modificacao, reverse=True)

for f in sorted_files:
    print(f)

Thus, the name of the files will be shown on the screen in order of modification, from the last modified to the first. If you want to use the creation date, switch to st_ctime, using data_criacao in key.

4

In view of Mr Woss' reply, I suggest a few modifications:

Instead of creating a function (lambda) for time (creation or modification):

data_criacao = lambda f: f.stat().st_ctime
data_modificacao = lambda f: f.stat().st_mtime

Make use of the native methods of os.path:

Example:

from pathlib import Path
from os.path import getmtime

directory = Path('/pasta/aonde/ficam/os/arquivos')
files = directory.glob('*.txt')
sorted_files = sorted(files, key=getmtime, reverse=True)

for f in sorted_files:
    print(f)

Something very important is that for the case of the file creation date, both the f.stat().st_ctime as to via checking os.path.getctime on some systems (like Unix) it will use the time metadata of the last modification of the file.

It is important to say that the use Path() despite facilitating between situations of WindowsPath and PosixPath, or even to "reuse", but does not seem to be the case (need), if all is well, but if not the use of glob.iglob(pathname, *, recursive=False) should be more than sufficient:

I quoted the iglob() instead of glob.glob(), for he, as well as the Path().glob(), return a iterator, which will consume less memory than the glob.glob() (that returns a list()).

If it’s something simple I could just stay:

from glob import iglob
from os.path import getmtime


files = iglob('/pasta/aonde/ficam/os/arquivos/*.txt')
sorted_files = sorted(files, key=getmtime, reverse=True)

for f in sorted_files:
    print(f)

If the goal is to get only the specific file and not list the folder, you can use the method max(), instead of sorted:

from pathlib import Path
from os.path import getmtime

directory = Path('/pasta/aonde/ficam/os/arquivos')
files = directory.glob('*.txt')

arquivo_mais_recente = max(files, key=getmtime)

print(arquivo_mais_recente)

or with iglob:

from glob import iglob
from os.path import getmtime


files = iglob('/pasta/aonde/ficam/os/arquivos/*.txt')

arquivo_mais_recente = max(files, key=getmtime)

print(arquivo_mais_recente)

Enough to use scandir() and with a for: plus a variable will check the previous value to see if it is smaller, through the os.path.getmtime with a simple if:

from os import scandir
from os.path import getmtime


files = scandir('/pasta/aonde/ficam/os/arquivos/')
mais_antigo = 0
arquivo_mais_recente = None

for entry in files:
    if entry.is_file() and entry.name.lower().endswith('.txt'):
        modificado_em = getmtime(entry.path)

        if mais_antigo < modificado_em:
            mais_antigo = modificado_em
            arquivo_mais_recente = entry.path

print(arquivo_mais_recente)

Which is not necessarily advantageous compared to Path().glob() or glob.iglob(), but helps to understand logic (which you can use if you are carrying something similar to a language that does not have something equivalent to iglob).


Another detail, that being just an additional, with Path().glob() to make the process recursively you can use **, example:

from pathlib import Path

directory = Path('/pasta/aonde/ficam/os/arquivos')
files = directory.glob('**/*.txt')

This way you will find both in the desired folder and in its subfolders. Already the equivalent in iglob would set the parameter recursive= as True:

from glob import iglob

iglob('/pasta/aonde/ficam/os/arquivos/*.txt', recursive=True)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.