Sep-28-2022, 10:15 PM
Another approach:
test conditions:
directory in same path as script, named 'filedir' (you can change as needed)
containing the following files:
test conditions:
directory in same path as script, named 'filedir' (you can change as needed)
containing the following files:
Output:igMess_9.csv
igMess_10.csv
igMess_11_.csv
igMess_12_.csv
igMess_13_.csv
from pathlib import Path import os os.chdir(os.path.abspath(os.path.dirname(__file__))) def check_for_highest(StartsWith, EndsWith, Directory): dir = Path(Directory) filelist = [filename for filename in dir.iterdir() \ if filename.is_file() and str(filename.stem).startswith(StartsWith) \ and str(filename.stem).endswith(EndsWith)] largest_name = sorted(filelist)[-1] return filelist, largest_name def testit(): filelist, largest = check_for_highest('igMess_', '_', 'filedir') for file in filelist: print(file) next_available = str(int(largest.stem[7:-1]) + 1) print(f"next available number = {next_available}") testit()results:
Output:filedir/igMess_11_.csv
filedir/igMess_12_.csv
filedir/igMess_13_.csv
next available number = 14