Sort files in a given folders and provide as a list I have two folders query and subject. The following function sorts alphabetically and provides the query and subject as a separate list. The file names are mixed of numbers and I found the sort function works perfectly well. Any comments to improve?

import os
import re


subject_path = "/Users/catuf/Desktop/subject_fastafiles/"
query_path = "/Users/catuf/Desktop/query_fastafiles"



def sorted_nicely(l):

    """ Sort the given iterable in the way that humans expect. https://blog.codinghorror.com/sorting-for-humans-natural-sort-order/ """
    convert = lambda text: int(text) if text.isdigit() else text
    alphanum_key = lambda key: [ convert(c) for c in re.split('([0-9]+)', key) ]
    return sorted(l, key = alphanum_key)


def subject_list_fastafiles():

    subject_fastafiles = sorted_nicely([fastafile for fastafile in os.listdir(subject_path) if os.path.isfile(os.path.join(subject_path, fastafile))])

    return subject_fastafiles

def query_list_fastafiles():

    query_fastafiles = sorted_nicely([fastafile for fastafile in os.listdir(query_path) if os.path.isfile(os.path.join(query_path, fastafile))])

    return query_fastafiles

def filter_files_ending_with_one(sorted_files):

    """ The function filters the files end with 1 """

    files_end_with_one = [name for name in subject_fastafiles if name[-1].isdigit() and not name[-2].isdigit() == 1]

    return files_end_with_one


subject_fastafiles = subject_list_fastafiles()

query_fastafiles = query_list_fastafiles()

subject_files_ending_with_one = filter_files_ending_with_one(subject_fastafiles)
Docstrings

You should include a docstring at the beginning of every function, class, and module you write. This will allow documentation to identify what your code is supposed to do. This also helps other readers understand how your code works. I see that you already have a couple for your functions, but stay consistent.

Parameter Names

Parameter names should be descriptive enough to be able to tell what should be passed. While l might be obvious to some programmers as an iterable, to others it might not. Since you're passing a list, renaming it to list_ (to avoid using reserved word list) makes it more obvious what you're passing, and accepting.

Constant Variables

When you have a constant in your program, it should be UPPER_CASE to identify it as such.

Code Reduction

You want as little code as possible in your program. So, instead of:

def subject_list_fastafiles():
    """ Method Docstring """
    subject_fastafiles = sorted_nicely([fastafile for fastafile in os.listdir(subject_path) if os.path.isfile(os.path.join(subject_path, fastafile))])
    return subject_fastafiles

def query_list_fastafiles():
    """ Method Docstring """
    query_fastafiles = sorted_nicely([fastafile for fastafile in os.listdir(query_path) if os.path.isfile(os.path.join(query_path, fastafile))])
    return query_fastafiles

def filter_files_ending_with_one(sorted_files):
    """ Method Docstring """
    files_end_with_one = [name for name in subject_fastafiles if name[-1].isdigit() and not name[-2].isdigit() == 1]
    return files_end_with_one


You can simply return the function call, instead of assigning it to a variable and returning the variable, like so:

def subject_list_fastafiles():
    """
    Method Docstring
    """
    return sorted_nicely([fastafile for fastafile in os.listdir(SUBJECT_PATH) if os.path.isfile(os.path.join(SUBJECT_PATH, fastafile))])

def query_list_fastafiles():
    """
    Method Docstring
    """
    return sorted_nicely([fastafile for fastafile in os.listdir(QUERY_PATH) if os.path.isfile(os.path.join(QUERY_PATH, fastafile))])

def filter_files_ending_with_one():
    """
    The function filters the files end with 1
    """
    return [name for name in SUBJECT_FASTAFILES if name[-1].isdigit() and not name[-2].isdigit() == 1]


Main Guard

This is an excerpt from this fabulous StackOverflow answer.

When your script is run by passing it as a command to the Python interpreter,

python myscript.py


all of the code that is at indentation level 0 gets executed.  Functions and classes that are defined are, well, defined, but none of their code gets run.  Unlike other languages, there's no main() function that gets run automatically - the main() function is implicitly all the code at the top level.

In this case, the top-level code is an if block.  __name__ is a built-in variable which evaluates to the name of the current module.  However, if a module is being run directly (as in myscript.py above), then __name__ instead is set to the string "__main__".  Thus, you can test whether your script is being run directly or being imported by something else by testing

if __name__ == "__main__":
    ...


If your script is being imported into another module, its various function and class definitions will be imported and its top-level code will be executed, but the code in the then-body of the if clause above won't get run as the condition is not met.

Updated Code

"""
Module Docstring (A description of your program goes here)
"""

import os
import re

SUBJECT_PATH = "/Users/catuf/Desktop/subject_fastafiles/"
QUERY_PATH = "/Users/catuf/Desktop/query_fastafiles"

def sorted_nicely(list_):
    """
    Sort the given iterable in the way that humans expect. https://blog.codinghorror.com/sorting-for-humans-natural-sort-order/
    """
    convert = lambda text: int(text) if text.isdigit() else text
    alphanum_key = lambda key: [convert(c) for c in re.split('([0-9]+)', key)]
    return sorted(list_, key=alphanum_key)

def subject_list_fastafiles():
    """
    Method Docstring
    """
    return sorted_nicely([fastafile for fastafile in os.listdir(SUBJECT_PATH) if os.path.isfile(os.path.join(SUBJECT_PATH, fastafile))])

def query_list_fastafiles():
    """
    Method Docstring
    """
    return sorted_nicely([fastafile for fastafile in os.listdir(QUERY_PATH) if os.path.isfile(os.path.join(QUERY_PATH, fastafile))])

def filter_files_ending_with_one():
    """
    The function filters the files end with 1
    """
    return [name for name in SUBJECT_FASTAFILES if name[-1].isdigit() and not name[-2].isdigit() == 1]

if __name__ == '__main__':

    SUBJECT_FASTAFILES = subject_list_fastafiles()
    QUERY_FASTAFILES = query_list_fastafiles()
    SUBJECT_FILES_ENDING_WITH_ONE = filter_files_ending_with_one()