Read directory listing of files and parse out the highest number? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: General Coding Help (https://python-forum.io/forum-8.html) +--- Thread: Read directory listing of files and parse out the highest number? (/thread-38312.html) |
Read directory listing of files and parse out the highest number? - cubangt - Sep-27-2022 So i have a directory where im placing files after my script runs, each file currently has a unique number in the file name. Example: igMess_'+ fileNumber+'_.csv' Currently i have the script hardcoded with the fileNumber variable set manually before running the script, its ok now because the directory only has about 10 files, so visually easy to see what the next number would be... What i would like to do is have a way to look in the directory for ONLY files that follow that naming convention and then determine the last/highest number so that my variable can be dynamic and use the next seq. number. Ive seen examples of using max(), but that doesnt seem to do what i thought it would, or at least not using it correctly. If my directory has the following files: igMess_1_.csv igMess_2_.csv igMess_3_.csv igMess_4_.csv igMess_5_.csv igMess_6_.csv temp1.txt temp2.txt mydata.xlsx i would like to ONLY consider the files that start with "igMess_" and end with "_.csv" and in the above example, the number i would like returned back would be "7" since that is the next seq number, so when my script runs, it names the new file as "igMess_7_.csv" would i still use max() in some way, or do i need to using something else? RE: Read directory listing of files and parse out the highest number? - cubangt - Sep-27-2022 new detail So it would def need to look at ONLY files that start with igMess and contain 2 underscores ( _ ) seems we have older files that are formatted like this: igMess_1.csv igMess_2.csv The newer format is: igMess_1_.csv igMess_2_.csv So only care to look at these and return the next highest number. I have this sample script im playing around with, but not sure how to only consider the files with 2 of those underscores. # iterate over files in # that directory for filename in os.listdir(directory): f = os.path.join(directory, filename) d = filename # checking if it is a file if d.startswith("igMess_"): print(d) RE: Read directory listing of files and parse out the highest number? - cubangt - Sep-27-2022 this code has gotten me closer, but still not sure how to get the max value. # iterate over files in # that directory for filename in os.listdir(directory): f = os.path.join(directory, filename) d = filename # checking if it is a file if d.startswith("igMess_"): if d.count('_'): if d.count('_') == 2: #print(d) temp = re.findall(r'\d+', d) res = list(map(int, temp)) print(str(res))this returns the following in my console. [1] [2] [3] []So im assuming that its not techanically a "list" where i can use max() RE: Read directory listing of files and parse out the highest number? - perfringo - Sep-28-2022 Just an idea: for every filename which ends with _.csv strip the specific characters, convert to int and find max value: filenames = ["igMess_2_.csv", "igMess_3_.csv", "igMess_1_.csv", "igMess_5.csv"] max_value = max(int(filename.strip('igMess_.csv')) for filename in filenames if filename.endswith("_.csv")) # max_value would in this example be 3 RE: Read directory listing of files and parse out the highest number? - cubangt - Sep-28-2022 well thats def simple and cleaner than what i was trying to do above. This logic works against my test directory filenames = os.listdir(directory) max_value = max(int(filename.strip('igMess_.csv')) for filename in filenames if filename.endswith("_.csv")) print(str(max_value)) RE: Read directory listing of files and parse out the highest number? - Larz60+ - Sep-28-2022 Another approach: test conditions: directory in same path as script, named 'filedir' (you can change as needed) containing the following files:
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:
|