Python glob - get latest file from list

I can get a list of files with similar names. What I'm trying to do is get a new file so that it can be manipulated. With glob, I can get all files, but not specific ones.

Here is my sample code:

permissionCurrentDate = '\n'.join(glob.iglob(os.path.join("PermissionsOnSystems*"))) 

Here is the result when I print it:

 PermissionsOnSystems2.txt PermissionsOnSystems20170313-144036.txt 

I want just PermissionsOnSystems20170313-144036.txt .

How can i do this?

Thanks!

+5
source share
1 answer

Depending on whether you want the latest file in terms of access time, metadata change time or change time, you can use os.path.getatime , os.path.getctime or os.path.getmtime . So something like:

 max(glob.iglob('PermissionsOnSystems*'), key=os.path.getmtime) 
+5
source

Source: https://habr.com/ru/post/1265401/


All Articles