Python Forum
Retrieving last 20 file paths from directory
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Retrieving last 20 file paths from directory
#1
Hello all,

I'm having a hard time getting this script across the finish line. I want to display only the last 20 file paths in the given directory, but I keep hitting a wall. Right now, it prints all the file paths in the folder, but I only want to the last 20. Can anyone help point me in the right direction?

import glob
import os
from pathlib import Path
path = (r'\\directory')
files = filter(lambda filepath: filepath.is_file(), Path(path).glob('*'))
for file in files:
   print(file.absolute())
Yoriz write Sep-24-2021, 01:09 PM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
Use code tags Code Tags.
Could do it like this.
import glob
import os
from pathlib import Path

path = (r'\\directory')
files = filter(lambda filepath: filepath.is_file(), Path(path).glob('*'))
file_lst = [f for f in files][-20:]
for file in file_lst:
    print(file)
Reply
#3
Awesome, thanks! Say I wanted to print the last 20 file paths from separate directories. For example, my setup would be:

(r'\\directory\asset1\asset1_runfile')
(r'\\directory\asset2\asset2_runfile')
(r'\\directory\asset3\asset3_runfile')
(r'\\directory\asset4\asset4_runfile')

How would I go about printing the last 20 file names of each "asset" without writing the same code with that slight variation over and over?
Reply
#4
(Sep-23-2021, 04:46 PM)dyerlee91 Wrote: I want to display only the last 20 file paths in the given directory, but I keep hitting a wall.

If you just use glob, the order is not alphabetical nor by modification time. Actually, I don't know if there is any kind of order. Maybe the inode-number define the order, but I am not certain.

You can use sorted with a key-function to define the order, and you can reverse it as well.
Her an example with sort_by_mtime, sort_by_ctime, sort_by_alpha:

from pathlib import Path


def sort_by_mtime(pathlike: Path):
    return pathlike.stat().st_mtime


def sort_by_ctime(pathlike: Path):
    return pathlike.stat().st_ctime


def sort_by_alpha(pathlike: Path):
    return str(pathlike.name)


def last_files(directory, pattern, last_files=10, key_function=None):
    
    path_iterator = Path(directory).glob(pattern)
    
    if callable(key_function):
        # if key_function is None or not callable, this branch is not executed
        sorted_iterator = sorted(path_iterator, key=key_function, reverse=True)
    else:
        # no key_function, so sorting by name alphanumeric implicit
        sorted_iterator = sorted(path_iterator, reverse=True)
    
    file_count = 0

    for path in sorted_iterator:
        if path.is_dir():
            # skipping directories
            continue
        
        if file_count >= last_files:
            return

        yield path
        
        file_count += 1
The function is a generator, you've to consume it.

files = list(last_files("/home/pi/Downloads", pattern="*.txt", last_files=5, key_function=sort_by_ctime))
# list files in Download
# with *.txt
# and the last 5 of it.
# the last file comes first in list, the second last file comes second ...
As you can see, you can also use functions as arguments. This function (sort_by_ctime) is called by sorted for each file to get a value back for comparison. This value could be an int or a str or something else, but must then always return the same data-type.

I hope this is not overcomplicated.
dyerlee91 likes this post
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how to solve the 'NO SUCH DIRECTORY OR FILE' in pandas, python MohammedSohail 10 15,361 May-08-2020, 07:45 AM
Last Post: nnk
  paths Scott 13 8,158 May-16-2018, 06:25 AM
Last Post: Scott

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020